SQl
DDL
DML
DCL
DQL
System Database
Master
Temdb
Model
MSDB
User defined database
Primary data file
Secondary data file
Transaction log file
Create Database
create table emp_piyush
(
EmployeeID int,
EmpName varchar(30),
DOB date,
city varchar(20),
state varchar(20),
)
Data Integrity
Entity integrity
Domain Integrity
Referential integrity
User-defined integrity
Primary key constraint ( can be used at single column or clubbing of
multiple column)
Unique constraint
Foreign key constraint
Check constraint (in,like,between)
Null constraint
Example
create table dep_piyush
(
DeptID varchar(10)constraint PS1 primary key,
DeptName varchar(30),
)
create table emp_piyush
(
empID varchar(10)constraint PS2 primary key,
empName varchar(30)not null,
DeptID varchar(10) constraint PS3 foreign key references
dep_piyush(DeptID),
DOb date,
city varchar(20) constraint chk1 check (city in
('Mumbai','Delhi','Lucknow')),
State varchar(20) constraint def1 default ('India')
)
Alter table emp_piyush add age int constraint x11 check (age between 20 and
50)
Insert Into dep_piyush values('do2','admin'),('d03','HR'),('d04','sales')
Insert into dep_piyush values('do1','SI'),('d05','L&D')
select * from dbo.dep_piyush
Insert into dep_piyush(deptid,deptname) values ('do8','risk')
Insert Into dbo.emp_piyush values
('E002','jack','d04','1990/04/24','Mumbai','india',30)
Insert into emp_piyush(empID,empName,city) values
('E003','jolly','lucknow')
select empID,empname from emp_piyush
select empid, deptid from emp_piyush where city='lucknow'
Update
update emp_piyush set DeptID='d03',dob='2005/05/12',age=22
where empID='E001'
Delete
Delete emp_piyush where DOb='2005/05/12'
Truncate
Truncate Table table_name
Rollback and commit
Concatanatio
select empName +' is located in'+ city from emp_piyush
Mathmatical operator
select age+10 as updated_age from emp_piyush
select age-10 as updated_age from emp_piyush
select age*10 as updated_age from emp_piyush
select age/10 as updated_age from emp_piyush
Comparison Operator
select empid, deptid from emp_piyush where age>='40'
select empid, deptid from emp_piyush where age<>'21' and city='lucknow'
Range Operator
select * from emp_piyush where age not between 20 and 40
select * from emp_piyush where age between 20 and 40
IN
select * from emp_piyush where city in('mumbai','delhi')
Like
_,%
select * from emp_piyush where empName like 'jo%'
select * from emp_piyush where empName like '_o%'
select * from emp_piyush where empName like '[a-j]o%'
select * from emp_piyush where empName like '[^a-e]o%'
Null
select * from emp_piyush where age is/is not Null
Order Change
select * from emp_piyush order by age
select * from emp_piyush order by age desc
select * from emp_piyush order by dob, age (multiple sort)
TOP
select top 2 * from emp_piyush order by age
Distinct
select distinct dob,empname from emp_piyush
Function
String
Charindex()
Left()
Len()
Trim()
Reverse(
Replace()
Stuff()
Substring()
Patindex()
Charindex()
Select charindex('a',empname)from emp_piyush
Left()
Select left(empname,2)from emp_piyush
Select left('hello',2)
Len()
Select len('hello')
Select len(empname)from emp_piyush
Trim()
Select ltrim ('
hello')
Select Rtrim ('
hello
')
reverse
Select reverse ('hello')
Select reverse(empname)from emp_piyush
Replace
Select replace (' hello ', ' ','')
Select replace(empname,'j','k')from emp_piyush
Stuff()
Select stuff(empname,1,1,'w')from emp_piyush
Select stuff ('hello',1,2,'AB')
Select stuff ('hello',1,3,'ABC')
Substring()
Select substring ('hello',1,3)
Select substring(empname,1,3)from emp_piyush
Patindex()
Select patindex ('%e%','heelo')
Select patindex('%j%',empName)from emp_piyush
Select patindex ('%e_','heeeo')
Upper()
Lower()
Difference()
Date Functions
Select GETDATE()
Select month(GETDATE())
Select day(GETDATE())
Select year(GETDATE())
Select dateadd(yyyy,20,GETDATE())
Select datename(yyyy,GETDATE())
Select datename(mm,GETDATE())
Select datepart(yy,GETDATE())
Select datediff(DY,'1964/09/12',GETDATE())
Select datediff(mm,'1964/09/12',GETDATE())
Select datename(dw,'2015/03/16')
Ascii
select ascii('1')
select char('65') reverse of ascii
Other mathematical Functions
select ABS(-.987)
select round(.987,2)
select floor(1.987)
select square(1.987)
select ceiling(1.987)
select sqrt(1.987)
select power(1.987,3)
select exp(1.987)
Other text Functions
select CAST(12 as varchar)+'welcome'
select CAST(12.34 as money )
select convert(varchar,11.23)+'welcome'
select convert(varchar,11.23)
select empid,empname,salary,rank()over(order by salary desc)as 'rank' from
employee
select empid,empname,salary,dense_rank()over(order by salary desc)as 'rank'
from employee
select empid,empname,salary,row_number()over(order by salary desc)as 'rank'
from employee
Miseclleneous function
select HOST_ID()
select HOST_name()
select db_id('SQLTRAINING')
select db_name('65')
Aggregate functions:
select MAX(salary)from emp_piyush where age>22
select avg(salary)from emp_piyush where age>22
select min(salary)from emp_piyush where age>22
select sum(salary)from emp_piyush where age>22
select deptid, sum(salary)from emp_piyush group by deptid
select deptid, count(salary)from emp_piyush group by deptid
select sum(salary)from emp_piyush group by deptid having SUM(salary)>20000
Example
create table pay_piyush
(
basic int,
hra int,
total as basic + hra
)
insert into pay_piyush values(10000,5000)
select * from pay_piyush
Copy the tables
select * into temp_piyush from emp_piyush
select empID,empName into temp2_piyush from emp_piyush
Drop the column from table
alter table pay_piyush drop column total
Joins
Cross join
select e.empid,e.empname,d.deptname from emp_piyush e cross join
dep_piyush d
Inner join
select e.empid,e.empname,d.deptname from emp_piyush e inner join dep_piyush
d on e.DeptID=d.DeptID
select e.empid,e.empname,d.deptname from emp_piyush e inner join dep_piyush
d on e.DeptID=d.DeptID where d.DeptName='HR'
Outer join
select e.empid,e.empname,d.deptname from emp_piyush e left outer join
dep_piyush d on e.DeptID=d.DeptID
select e.empid,e.empname,d.deptname from emp_piyush e right outer join
dep_piyush d on e.DeptID=d.DeptID
select e.empid,e.empname,d.deptname from emp_piyush e full outer join
dep_piyush d on e.DeptID=d.DeptID
Self join
Sub Query
select empname from emp_piyush where salary=(select MAX(salary)from
emp_piyush)
select empname from emp_piyush where salary not in (select salary from
emp_piyush where DeptID='d04')
select empname from emp_piyush where salary in (select salary from
emp_piyush where DeptID='d04')
select empname from emp_piyush where exists (select salary from emp_piyush
where DeptID='d04')
select empname from emp_piyush where salary > any (select salary from
emp_piyush where city='mumbai')
Example
select e.empid,e.empname,d.deptid,p.projectid,p.projname from emp_piyush e
join dep_piyush d
on e.DeptID=d.deptid join project_piyush p
on e.empID=p.empid