AWD
-: Final Assignment :-
SQL Queries
1. To create a database:-
create database technoholics
2. To create a table:-
create table employee6 (id int, name varchar(50), age int, salary
int)
3. To see the contents of table:-
select * from employee
4. To insert new data into table:-
insert into employee values(4,'mayank',25,10000)
5. To edit data in table:-
update employee set id=10 where id=1
6. To delete a selected part in table like a row:-
delete from employee where id=10
7. To delete complete table:-
delete from employee
1
AWD
8. To remove the existence of table:-
drop table employee
9. To drop column:-
alter table employee6 drop column salary
10. To add column:-
alter table employee6 add salary int
11. To add a new data in a particular column:-
update employee6 set salary=10000 where id=1
12. To change data type of any column:-
alter table employee6 alter column salary varchar(50)
13. To see particular columns:-
select id,name from employee6
14. To see particular row like row with id=1
select * from employee6 where id=1
15.Different operators:-
<>:-not equal to
2
AWD
<=:-less tahn equal to
>=:-greater than equal to
>:-greater than
<:-less than
16. To use OR operator:-
select * from employee6 where id=1 or name='manish'
17. To use AND operator:-
select * from employee6 where id=1 and name='manish'
18. To use in operator:-
select * from employee6 where id in(3,2)
19. To use between:-
select * from employee6 where id between 2 and 3
20. To select name with 6 characters:-
select * from employee6 where name like '______'
21. To.select name with 6characters starting with m:-
select * from employee6 where name like 'm_____'
3
AWD
22. To select name with first letter m and next anything:-
select * from employee6 where name like 'm%'
23. To select name with first letter either m or a and next
anything:-
select * from employee6 where name like '[ma]%'
24. To select name with first character is not m:-
select * from employee6 where name like '[^m]%'
25. To select name with first character m or a and second
character a or o and next anything:-
select * from employee6 where name like '[ma][ao]%'
26. To select the rows with salary null:-
select * from employee6 where salary is null
27. To select in ascending order:-
select * from employee6 order by id
28. To select in descending order:-
select * from employee6 order by id desc
Use of FUNCTIONS:-
29. To use max:-
4
AWD
select max(id) from employee6
30. To give name to a column name after using max function:-
select max(id)as 'max id' from employee6
31. To use min:-
select min(id) from employee6
32. To use sum:-
select sum(id) as 'max id' from employee6
33. To use avg:-
select avg(id) as 'max id' from employee6
34. To use count:-
select count(*) as 'max id' from employee6
35. To display all columns with constraint that id should be
maximum :-
select age,max(id) as 'max id'from employee6 group by age
36. To display third highest age:-
select max(age) from employee6 where age <(select max(age)
from employee6 where age < (select max(age)from employee6))
5
AWD
37. To display the first three ages:-
select age from employee6 where age>=(select max(age) from
employee6 where age <(select max(age) from employee6 where
age < (select max(age)from employee6)))
38. To display the third minimum:-
select min(age) from employee6 where age>(select min(age)from
employee6 where age>(select min(age) from employee6))
39. To display the last three minimum:-
select age from employee6 where age<(select min(age) from
employee6 where age>(select min(age)from employee6 where
age>(select min(age) from employee6)))
40. To display all leaving first three maximum:-
select * from employee6 where age<(select max(age) from
employee6 where age<(select max(age) from employee6 where
age<(select max(age)from employee6)))
41. To use a different name for id i.e.max id alongwith original
name i.e.id:-
select id as 'max id' ,* from employee6 where id=(select max(id)
from employee6)
42. To create a view :-
6
AWD
create view secondmaxemployee as select max(age)as 'second
max' from employee6 where age<(select max(age)from
employee6)
43. To see the result of a view:-
select * from secondmaxemployee
Practice of 1st day
create database technoholics
create table employee5 (id int, name varchar(50), age int, salary
int)
select * from employee5
insert into employee5 values(5,'manish',21,17500);
update employee5 set age=21 where id=4
delete from employee5 where id=5
7
AWD
delete from employee6
drop table employee
alter table employee5 drop column phone_no
alter table employee5 add phone_no int
select * from employee5
alter table employee5 alter column salary varchar(50)
insert into employee5 values(5,'manish',21,'rs21000');
select * from employee5 where id<>1
select * from employee5 where id<=3
select * from employee5 where id>3
select * from employee5 where name='sajal' and id=2
select * from employee5 where name='sajal' or id=2
select * from employee5 where id not in(1,3,4)
select * from employee5 where salary between '17000' and '18500'
select * from employee5 where name like '______'
select * from employee5 where name like '%r%'
select * from employee5 where name like '[^ask]%'
select * from employee5 where name like '_[ask]_[jiuoa]%'
select * from employee5 where name like 's_j%'
8
AWD
select * from employee5 where salary is null
alter table employee5 add address int
select * from employee5 where address is null
update employee5 set address=12314 where id=3
select * from employee5
select * from employee5 where address is not null
select * from employee5 order by age
select * from employee5 order by id desc
select * from employee5 order by salary desc
Functions:-
select max(id) from employee5
select max(id) as 'max id' from employee5
select max(salary) from employee5
select min(id) as 'min id' from employee5
select sum(id) from employee5
select avg(id) as 'min id' from employee5
select count(*) as 'min id' from employee5
select * from employee5
9
AWD
select max(id) from employee5 where id<(select max(id) from
employee5 where id<(select max(id) from employee5))
select * from employee5 where id<(select max(id) from
employee5 where id<(select max(id) from employee5 where
id<(select max(id) from employee5)))order by id
select * from employee5 where id>=(max(id))
select min(id) from employee5
select min(id) from employee5 where id>(select min(id) from
employee5)
select * from employee5 where id<=(select min(id) from
employee5 where id>(select min(id) from employee5 where
id>(select min(id) from employee5)))
select id,name from employee5 where id<=(select min(id) from
employee5 where id>(select min(id) from employee5 where
id>(select min(id) from employee5)))
select * from employee5
update emp1 set id=5 where id=4
10
AWD
select * from employee5 where id=(select max(id) from
employee5)
select age as 'max id',* from employee5 where id=(select max(id)
from employee5)
select max(id) from employee5 where age=21
select * from employee5 where salary=(select max(salary) from
employee5 where age=21)
select * from employee5 order by salary desc
update employee5 set salary='r21000' where id=6
create view secondmaxemployee1 as select * from employee5
where id=(select max(id)as 'secondmax' from employee5 where
id<(select max(id) from employee5)
select * from secondmaxemployee1
create view secondmaxemployee as select max(id)as 'secondmax'
from employee5 where id<(select max(id) from employee5)
11
AWD
select * from secondmaxemployee
insert into secondmaxemployee values(12);
Practice of 2nd day
create database sajal
create table emp1(id int, name varchar(50), age int)
create table emp2(id int, salary int)
insert into emp1 values(1,'sajal',21)
insert into emp1 values(2,'manish',22)
insert into emp1 values(3,'karan',23)
insert into emp1 values(4,'minakshi',20)
select * from emp1
insert into emp2 values(1,20000)
insert into emp2 values(2,22000)
insert into emp2 values(3,23000)
insert into emp2 values(4,20000)
12
AWD
select * from emp2
select * from emp1
1.equi Join:-
select emp1.id, emp1.name, emp1.age, emp2.salary from emp1,
emp2 where emp1.id=emp2.id
2.INNER JOIN:-
select emp1.id, emp1.name, emp1.age,emp2.salary from emp1
inner join emp2 on emp1.id=emp2.id
3.LEFT OUTER JOIN:-
select emp1.id, emp1.name, emp1.age, emp2.salary from emp1 left
outer join emp2 on emp1.id=emp2.id
4. RIGHT OUTER JOIN:-
select emp1.id, emp1.name, emp1.age, emp2.salary from emp1
right outer join emp2 on emp1.id=emp2.id
5. FULL JOIN:-
select emp1.id, emp1.name, emp1.age, emp2.salary from emp1
full join emp2 on emp1.id=emp2.id
alter table emp1 drop column age
create table emp3(id int, age int)
insert into emp3 values(1,20)
insert into emp3 values(2,21)
13
AWD
insert into emp3 values(3,22)
insert into emp3 values(4,23)
insert into emp3 values(5,19)
insert into emp3 values(6,25)
select * from emp1
select * from emp2
select * from emp3
1.equi Join on 3 tables:-
select emp1.id as 'emp1 id',emp2.id as 'emp2 id',emp3.id as 'emp3
id',emp1.name,emp2.salary,emp3.age from emp1,emp2,emp3
where emp1.id=emp2.id and emp1.id=emp3.id
2.INNER JOIN on 3 tables:-
select emp1.id as 'emp1 id',emp2.id as 'emp2 id',emp3.id as 'emp3
id',emp1.name,emp3.age,emp2.salary from emp1 inner join emp2
on emp1.id=emp2.id inner join emp3 on emp2.id=emp3.id
3. LEFT OUTER JOIN on 3 tables:-
select emp1.id as 'emp1 id',emp2.id as 'emp2 id',emp3.id as 'emp3
id',emp1.name,emp3.age,emp2.salary from emp1 left outer join
emp2 on emp1.id=emp2.id left outer join emp3 on
emp1.id=emp3.id
4. RIGHT OUTER JOIN on 3 tables:-
select * from emp1
14
AWD
select * from emp2
select * from emp3
select emp1.id as 'emp1 id',emp2.id as 'emp2 id',emp3.id as 'emp3
id',emp1.name,emp2.salary,emp3.age from emp1 right outer join
emp2 on emp1.id=emp2.id right outer join emp3 on
emp2.id=emp3.id
select emp1.id as 'emp1 id',emp2.id as 'emp2 id',emp3.id as 'emp3
id',emp1.name,emp2.salary,emp3.age from emp2 right outer join
emp1 on emp2.id=emp1.id right outer join emp3 on
emp1.id=emp3.id
5. FULL JOIN:-
select emp1.id as 'emp1 id',emp2.id as 'emp2 id',emp3.id as 'emp3
id',emp1.name,emp2.salary,emp3.age from emp3 full join emp2 on
emp3.id=emp2.id full join emp1 on emp1.id=emp3.id
select emp1.id as 'emp1 id',emp2.id as 'emp2 id',emp3.id as 'emp3
id',emp1.name,emp2.salary,emp3.age from emp1 full join emp2 on
emp1.id=emp2.id full join emp3 on emp2.id=emp3.id
create view equijoinview as select emp1.id as 'emp1 id',emp2.id as
'emp2 id',emp3.id as 'emp3 id',emp1.name,emp2.salary,emp3.age
from emp1,emp2,emp3 where emp1.id=emp2.id and
emp1.id=emp3.id
select * from equijoinview
15
AWD
create table employee(employee_id int, name varchar(50))
create table orders(prod_id int, products varchar(50), employee_id
int)
insert into employee values(01,'Hansen, Ola')
insert into employee values(02,'Svendson, Tove')
insert into employee values(03,'Svendson, Stephen')
insert into employee values(04,'Pettersen, Kari')
insert into orders values(234,'Printer',01)
insert into orders values(657,'Table',03)
insert into orders values(865,'Chair',03)
select * from employee
select * from orders
select * from emp1
insert into emp1 values(5,'')
insert into emp1 values(5,null)
insert into emp1 values(null,'sajal')
delete from emp1 where
insert into emp1(id)values(5)
16
AWD
select * from emp1
select * from emp2
insert into emp1(emp1.id,emp1.name), emp2(emp2.salary)
values(6,'sajal',50000)
insert into emp1(id,name) emp2(salary) values(6,'sajal',50000)
create table emptemp
(id int, name varchar(50), salary int)
select * from emptemp
delete from emptemp
insert into emptemp values(8,'man',600)
insert into emp1 select id,name from emptemp
insert into emp2 select id,salary from emptemp
insert into emp1(id,name) values(10,'sajal')
insert into emp2(id,salary) values(10,1000)
insert into emp1 values(11,'sam')
insert into emp2 values(select last(id) from emp1,)
create view insertrec as insert into
insert into emp1 values(11,'sam')
select @@identity
create table oddnumber(num int)
i=-1
17
AWD
insert into oddnumber select max(num)+2 from oddnumber
between 1 to 100
select * from oddnumber
Practice of 3rd
RELATIONSHIP:-
1.one to one
2.one to many
3.many to many
One to Many:-
Teacher:- tid, name
Relation
Students :- sid, sname
18
AWD
One to One:-
Students:- sid, sname
Relation
Project:- pid, pname
Many to Many:-
Products:- pid, pname
Relation
Customer:- cid, cname
CONSTRAINTS:-
1. UNIQUE (no repated values are allowed)
2. NOT NULL (no null value allowed)
3. PRIMARY KEY (contain feature of both notnull & unique)
4. CHECK (to check some constraints)
5. DEFAULT (for default value)
6. foreign key (tid int foreign key references teacher(tid))
create table teacher(tid int primary key, tname varchar(50))
create table student(sid int primary key, sname varchar(50), tid int
foreign key references teacher(tid))
19
AWD
select * from teacher
select * from student
insert into teacher values(1,'punnet')
insert into student values(2,'sajal',1)
insert into teacher(tid) values(2)
insert into student values(1,'sanjay',2)
insert into student(sid,sname) values(6,'karan')
create table product(pid int primary key, pname varchar(40))
create table customer(cid int primary key, cname varchar(40))
create table relation(pid int foreign key references product(pid) ,
cid int foreign key references customer(cid))
select * from customer
select * from product
select * from relation
insert into customer values(1,'sajal')
insert into customer values(2,'sanjay')
insert into product values(1,'bmw')
insert into product values(3,'mercerdies')
insert into relation values(1,1)
insert into relation values(1,1)
insert into relation values(1,2)
insert into relation values(3,1)
20
AWD
insert into relation values(3,2)
Example:-
on delete cascade
on update cascade
after foreign key references we have to write these two lines
create table teacher5(tid int primary key, tname varchar(50))
create table student5(sid int primary key, sname varchar(50), tid
int foreign key references teacher(tid) on delete cascade on update
cascade)
Practice of 4th day
select * from teacher
select * from student5
insert into teacher values(1,'sanjay')
insert into teacher values(2,'sajal')
insert into student5 values(1,'king',1)
insert into student5 values(4,'sujata',2)
delete from teacher where tid=10
update teacher set tid=10 where tid=1
select * from teacher5
drop table student
21
AWD
create table cons1(id int unique, name varchar(25), salary int
default 17500)
select * from cons1
alter table cons1 drop constraint UQ__cons1__173876EA
insert into cons1 values(1,'sajal','')
insert into cons(id,name) values(2,'sanjay')
insert into cons values(3,'karan',20000)
select * from cons
alter table cons alter column salary int
insert into cons(id,name) values(4,'manish')
insert into student5 values(5,'sujata',5)
alter table student5 drop constraint
FK__student5__tid__1273C1CD
alter table student5 add constraint fk12 foreign key(tid) references
teacher(tid) on update cascade on delete set null
delete from teacher where tid=1
select * from teacher
select * from student5
22
AWD
alter table student5 drop constraint fk12
alter table student5 add constraint fk12 foreign key(tid) references
teacher(tid) on delete set select max(tid) from teacher
To add & delete diff constraints:-
create table conn(id int, name varchar(40), age int, salary int)
alter table conn add constraint not null(name)
alter table conn add constraint pk primary key(id)
alter table conn add constraint ck check (age>=18)
alter table conn add constraint uq unique(name)
alter table conn add constraint df default 17000 for salary
alter table conn alter column id int not null
alter table conn add constraint pk primary key(id)
alter table conn alter column id int constraint nn not null
insert into conn values(1,1,18,1)
insert into conn(id,name,age) values(2,'s',18)
select * from conn
23
AWD
NOW DELETE ALL CONSTRAINTS:-
alter table conn drop constraint pk
alter table conn drop constraint uq
alter table conn drop constraint ck
alter table conn drop constraint df
insert into conn values(1,1,18,1)
insert into conn(id,name,age) values(4,'s',18)
select * from conn
Foreign key add delete:-
select * from conn
delete conn sp_help
alter table conn add constraint pk primary key(id)
create table conn2(cid int, id int )
alter table conn2 add constraint fk foreign key(id) references
conn(id)
create table conn3(cid int constraint nn not null, id int)
24
AWD
alter table conn3 drop constraint nn
Practice of 5th day
STORED PROCEDURE:-
create table student25(id int, name varchar(25), age int, salary int)
insert into student25 values(1,'raja',24,18000)
INSERT procedure:-
create procedure inserttemp (@ename varchar(25), @eage int,
@esalary int) as insert into student25 values (@eid, @ename,
@eage, @esalary)
execute inserttemp 2,'king',25,19100
select * from student25
execute inserttemp 1,'raja',21,211111
execute inserttemp 2,2,'2',2
execute inserttemp 2,2,'2a',2 'error'''''''''''
25
AWD
Update procedure:-
create procedure updateidtemp(@neid int, @oeid int)as
update student25 set id=@neid where id=@oeid
select * from student25
execute updateidtemp 4,1
create procedure updatenameastemp(@eid int, @ename varchar
(25), @eage int, @esalary int) as
update student25 set name=@ename,age=@eage,salary=@esalary
where id=@eid
select * from student25
execute updatenameastemp 2,'sajal',21,25000
26
AWD
Delete procedure:-
create procedure deletetemp(@eid int)as
delete from student25 where id=@eid
select * from student25
execute deletetemp 4
Select procedure:-
create procedure selecttemp1(@eid int)as
select * from student25 where id=@eid
select * from student25
execute selecttemp1 1
delete from student25 where name='raja' and age=21 and
salary=211111
create procedure select2(@ename varchar(25), @esalary int) as
select * from student25 where name=@ename or salary=@esalary
27
AWD
execute select2 'sajal',19100
select * from student25
ALTER PROCEDURE:-
alter procedure selecttemp1(@ename varchar(25))as
select * from student25 where name=@ename
execute selecttemp1 raja
execute selecttemp1 sajal
CREATE ALTER PROCEDURE:-
create table student255(id int, name varchar(25),age int, salary int)
create procedure altertemp1 as alter table student255 drop column
salary execute altertemp1
select * from student255
create procedure altertemp2add as alter table student255 add salary
varchar(25)
execute altertemp2add
The END
28