SQL P2 (SQL)
SQL P2 (SQL)
Language
Lets do practical on DATABASE…
SQL– Structured Query Language
Is a language that enables you to create and operate on
relational databases
It is the standard language used by almost all the
database s/w vendors.
Pronounced as SEQUEL
Original version was developed by IBM’s Almanden
Research Center
Latest ISO standard of SQL was released in 2008 and
named as SQL:2008
SQL – Features
Querying database
Literals
Data types
Nulls
Comments
Literals
It means the fixed value or constant value. It may be of
character, numeric or date time type.
FLOAT(M,D) Real numbers i.e. number with decimal. M specify length of numeric value
including decimal place D and decimal symbol. For example if it is given as
FLOAT(8,2) then 5 integer value 1 decimal symbol and 2 digit after decimal
TOTAL – 8. it can work on 24 digits after decimal.
DECIMAL It is used to store exact numeric value that preserve exact precision for e.g.
money data in accounting system.
DECIMAL(P,D) means P no. of significant digits (1-65), D represent no. of digit after
decimal(0-30), for e.g DECIMAL(6,2) means 4 digit before decimal and 2 digit after
decimal. Max will be 9999.99
Date and Time Types
Data type Description
DATETIME Combination of date and time. For example to store 4th December
2018 and time is afternoon 3:30 then it should be written as –
2018-12-04 15:30:00
TIMESTAMP Similar to DATATIME but it is written without hyphen for example the
above date time is stored as 20181204153000
YEAR(M) To store only year part of data where M may be 2 or 4 i.e. year in 2
digit like 18 or 4 digit like 2018
String Types
Data type Description
CHAR(M) Fixed length string between 1 and 255. it always occupy M size for each
data for example if size is CHAR(20) and we store value ‘MOBILE’ ,
although the size of MOBILE is 6 but in a table it will occupy 20 size with
space padded at right side for remaining place.
Mostly use in the case where the data to be insert is of fixed size like
Grade (A,B,C,..) or Employee code as E001, E002, etc. In this case CHAR
will give better performance than varchar
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
Fast, no memory allocation every time Slow, as it take size according to data so
every time memory allocation is done
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.
To Create a Database :
CREATE DATABASE <DATABASE NAME>
Eg. CREATE DATABASE MYDB;
Example:-
Create Table emp (empno int, name varchar(20),
dept varchar(20), salary int);
Note:-
1) char, varchar and date value must be in single
quotes
Syntax:-
For Example :-
Select 10*2;
Select 10*3/6;
PERFORMING SIMPLE
CALCULATION
MySQL also provides DUAL table to provide compatibility with
other DBMS. It is dummy table used for these type queries
where table name is not required. It contains one row and one
column. For example:
Select 100+200 from DUAL;
Select curdate() from dual;
Example :-
(i) Select empno Employee_Number, name, dept Department,
Salary Income 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;
For e.g.
1)BETWEEN
2)IN
3)LIKE
4)IS NULL
BETWEEN Operator
BETWEEN allows to specify range of values to search in
any column. It is used with AND clause and it will include
the specified values during the searching.
For e.g.
Select * from emp where salary NOT between 25000 and 35000;
IN operator
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.
whereas SYSDATE( ) will always return date and time at which each SYDATE()
function started execution. For example.
mysql> Select now( ), sleep(2), now( );
Output: 2018-12-04 10:26:20, 0, 2018-12-04 10:26:20
mysql> Select sysdate(), sleep(2), sysdate();
Output: 2018-12-04 10:27:08, 0, 2018-12-04 10:27:10
AGGREGATE FUNCTIONS
AGGREGATE FUNCTION is used to perform calculation
on group of rows and return the calculated summary like
sum of salary, average of salary etc.
Note:
All aggregate function ignores the NULL
values.
GROUP BY
GROUP BY clause is used to divide the
table into logical groups and we can
perform aggregate functions in those
groups. In this case aggregate function
will return output for each group.
SELECT JOB,SUM(SAL),AVG(SAL),MAX(SAL),
COUNT(*) EMPLOYEE_COUNT FROM
EMP;
E.g.
• SELECT DEPT,AVG(SAL) FROM EMP GROUP BY DEPT
HAVING JOB IN (‘HR’,’SALES’)