SQL Notes
SQL Notes
SQL Notes
Data
It is a useful information which is stored in database.
Database
All the useful information stored in a common place is called as database.
DBMS(database management system)
It is a collection of programs written in order to manage the database
effectively is called as DBMS.
Management means: storing data, retrieve data, delete data, update data,
insertion of data.
By the use of DBMS we can interact with the database.
Table structure
Note :
1. SQL is not case sensitive , but data present in SQL are case sensitive.
2. Column name & table name needs to be used as it is.
Examples :
• Select ename as name ,Job as desg from emp;
• Select ename as “name” from emp;
Note :
For column aliasing, as keyword is optional
Syntax
Select column name from table name table name;
Examples :
• Select ename from emp e;
Note :
For table aliasing, as keyword is not necessary.
Query writing :
1. Query to display employee name & salary which has been increased by
1000/- for all the employees
Select ename, sal+1000 as isal from emp;
2. Query to display employee name, annual salary , daily salary for all the
employees
Select ename, sal*12 as asal, sal/30 as dsal from emp;
3. Query to display employee name, designation& salary where it has been
decreased by 500/- for all the employees
Select ename, job, sal-500 as dsal from emp;
String
Date
• We can use literals when we want to print same value in each & every row
• Number can be written directly without single quotes
• If we want to use string & date kind of data, it should be used within single quotes
Examples :
1. Select 5 from emp;
2. Select ‘01-Jan-08’ from dept;
3. Select ‘SQL’ from dept;
Example:
select 'my name is' || ename from emp;
Syntax:
Select column name from table name order by column name asc;
Select column name from table name order by column name desc;
Note:
By default output will be displayed in ascending order.
It is optional that we can mention “asc” keyword in the query.
1.Query to display employee name, annual salary, daily salary for all the
employees and rearrange the o/p as per descending order from annual salary.
select ename,sal*12 as asal,sal/30 dsal from emp order by asal desc;
2. Query to display all the information of an employee and rearrange the o/p of
hiredate as per their seniority.
select * from emp order by hiredate asc;
Note:
Execution order of order by
select 2
from 1
order by 3
Execution flow:
Select 3
From 1
Where 2
Order by 4
Examples:
1. Query to display all the information of an employee whose salary is more
than 2000.
select * from emp where sal> 2000;
2. Query to display all the information of an employee who is earning a salary
below 1500.
select * from emp where sal<1500;
3. Query to display all the information of an employee who is earning a salary of
Rs.3000.
select * from emp where sal=3000;
Example:
1. Write a query to display managers and analyst information who belongs to
deptno 30.
select * from emp where job in('MANAGER','ANALYST') and deptno=30;
Between operator:
It is used when the given values are within the range.
Examples:
1. Write a query to display the employee’s name whose name is starting with
letter “S”.
select * from emp where ename like 'S%';
2. Query to display the employee name and designation whose designation has
man word .
select ename, job from emp where job like '%MAN%‘;
3. Query to display all the information of employee whose salary last but one
digit is 5.
select * from emp where sal like '%5_';
Examples:
1. Write a query to display all the information of an employee whose
commission is null.
select * from emp where comm is null;
2. Write a query to display all the information of an employee whose commission
is not null.
select * from emp where comm is not null;
Examples:
Examples:
Examples:
1. Write a query to display the length of every employee name.
select length(ename) from emp;
2. Write a query to display employee name who’s name is having exactly 6
characters.
select ename from emp where length(ename)=6;
3. Write a query to count number of “L” present in ename.
select length(ename)-length(replace(ename , 'L')) from emp;
Examples:
1. Write a query to display unique job from employee table.
select distinct(job) from emp;
2. Write a query to display distinct department number from employee.
select distinct(deptno) from emp;
Note: Below query is not possible.
select distinct(job),distinct(deptno) from emp;
Note:
Only for count function we can use * . Example: count(*)
example:
select max(sal) ,deptno from emp group by deptno;
Order of execution:
Select 3
From 1
Group by 2
Order of execution:
Select 5
From 1
Where 2
Group by 3
Having 4
Order by 6
Note:
Common column should exist between the table to write sub queries.
When indirect condition is in same table then we can refer same column or
dependence column information.
In inner query we cant use “*”
Type of joins:
1. Cross or Cartesian joins
2. Equi joins
3. Self joins Inner joins Fetches only matched records
Syntax:
Select column name from table name JOIN table name on condition;
2. Alter – add
To add columns in the existing table.
Syntax:
Alter table table name
add(column name datatype (size));
Note: Truncate is used to drop only the records and table structure remains the
same. Truncated information cannot be retrieved back.
Syntax:
Truncate table table name;
commit; (TCL)
2. Roll Back:
It will check for the previous commit if it is not found directly goes to the
auto commit.
ROLL BACK;
3. Flashback:
Using flash back we can get back the dropped tables from the recycle bin.
Syntax:
flashback table table name to before drop;
Example:
drop table emp42;
show recyclebin;
Purge recyclebin;
Note:
After granting permissions to the HR he can access the tables using the
below command.
select * from scott emp; (use it in HR account)
Normalization:
It is a set of rules, its not mandatory to follow the normalization rules, but if
we follow the normalization rules it is a better practice.
Normalization rules are followed while creating the table to the database.