Structured Query Language: Lets Do Practical On DATABASE
Structured Query Language: Lets Do Practical On DATABASE
VARCHAR(M) Variable length string between 1 and 65535 (from MySQL 5.0.3) , earlier it was
255. it takes size as per the data entered for example with VARCHAR(20) if the
data entered is MOBILE then it will take only 6 byte. It is useful for the data like
name, address where the number of character to be enter is not fixed.
VARCHAR2 It is supported in ORACLE, both are almost same with minor difference. The
difference is in the way they are handling Empty String and NULL, for VARCHAR
these two are different where as VARCHAR2 treats both same.
Difference between CHAR & VARCHAR
CHAR VARCHAR
Fixed length string Variable length string
Used where number of character to enter Used where number of character to be
is fixed like Grade, EmpCode, etc enter is not fixed like name, address etc.
Fast, no memory allocation every time Slow, as it take size according to data so
every time memory allocation is done
It takes more memory It takes less space
NULL VALUE
• NULL means missing information
• NULL can appear in any type of column if it is not restricted by
NOT NULL or PRIMARY KEY
• Always remember NULL is neither equal to 0 nor space. NULL
means nothing
• Used in situation like if email id is not available with students
then we will insert NULL
COMMENTS
• It is a text that is not executed, only for
documentation purpose. Comments in MySQL
can be written as
– Begin the comment with /* and */
– Begin the comment with – (followed by space)
– Begin then comment with #
• For example
– /* Select * from emp where empno=4 */
– Select * from emp; -- it will fetch all details
SQL COMMAND SYNTAX
Commands Description
Keywords That have special meaning in SQL. They are the commands in mysql
Clause They are used to support mysql commands like FROM, WHERE etc.
Arguments Values passed to clause like table name to FROM clause conditions to
WHERE clause for e.g.
SELECT * FROM EMP WHERE SALARY>12000;
In the above command
SELECT is keyword
FROM AND WHERE is clause
EMP is an argument to FROM
SALARY>12000 is argument
to WHERE
CREATING and USING DATABASE
CREATE DATABASE <DATABASE NAME>
CREATE DATABASE MYDB;
Note:-
1) char, varchar and date value must be in
single quotes
2) Values must be passed in the order of their column
3) Date values are passed in the format
dd-mon-yyyy i.e. 20-Sep-2015 (in oracle)
yyyy-mm-dd (in mysql)
INSERTING RECORDS IN TABLE
Syntax:-
selected columns
Insert into emp (empno, name, dept ) values
(2,’dipanker’,’IT’)
SELECTING RECORD
Select statement allows to send queries to table and
fetch the desired record. Select can be used to select
both horizontal and vertical subset.
Syntax:-
Select * / columnnames FROM
tablename [ where condition ]
SELECTING RECORD
Selecting all record and all columns
Select * from emp;
From the above table we can observe that salary of Shaban is NULL i.e.
not assigned, Now if we want 0 or “not assigned” for the salary
information of shaban, we have to use IFNULL()
Select empno,name,IFNULL(Salary,”not assigned”) from emp;
Select * from emp where salary NOT between 25000 and 35000
IN
IN allows to specify LIST of values in which searching will be
performed. It will return all those record that matches any value in
a given list of values. It can be thought as an alternative of multiple
ORs
! HIGH
*, /, DIV, %, MOD
-+
<, >
==, >=, <=, !=, IS, LIKE, IN, BETWEEN
NOT
AND
OR LOW
SORTING OUTPUT
By default records will come in the output in the same order in
which it was entered. To see the output rows in sorted or arranged
in ascending or descending order SQL provide ORDER BY clause.
By default output will be ascending order(ASC) to see output in
descending order we use DESC clause with ORDER BY.
NOTE :- when we are using GROUP BY we can use only aggregate function and the
column on which we are grouping in the SELECT list because they will form a group other
than any column will gives you an error because they will be not the part of the group.
For e.g.
SELECT ENAME,JOB,SUM(SAL) FROM EMP GROUP BY JOB;
Error -> because Ename is not a group expression
HAVING with GROUP BY
• If we want to filter or restrict some rows from the output produced by GROUP BY then
we use HAVING clause. It is used to put condition of group of rows. With having clause
we can use aggregate functions also.
• WHERE is used before the GROUP BY. With WHERE we cannot use aggregate function.
• E.g.
• SELECT DEPT,AVG(SAL) FROM EMP GROUP BY DEPT HAVING JOB IN
(‘HR’,’SALES’)