You are on page 1of 12

SQL PRACTICAL FILE QUESTIONS BY SAURABH SINGH NARUKA fuEufyf[kr Table dks mi;ksx es ysrs gq, uhps fn;s

x, izuks ds mRrj fnft,A Table: Employee No 1 2 3 4 5 6 7 Name Mukul Kritika Naveen Uday Nupur Moksh Shelly Salary 30000 Zone West Age 28 30 40 38 26 28 26 Grade A A C B A Dept 10 10 20 30 20 10 30

35000 Centre 32000 West 38000 North 32000 East 37000 36000 South North

Table: Department Dept 10 20 30 DName Sales Finance Admin MinSal 25000 30000 25000 MaxSal 32000 50000 40000 HOD 1 5 7

fuEufyf[kr izuks fd SQL Commands fyf[k,A izUk1% fn xbZ Table dks rS;kj fdft, rFkk bl es fn, xbZ Tuples insert djkb,A ans (1): CREATING TABLE EMPLOYEE AND DEPARTMENT: SQL> create table employee (no int primary key, name varchar(30), salary int, zone varchar(30), age int, grade varchar(5), dept int); SQL> create table department ( dept int, dname varchar(30), minsal int, maxsal int, hod int); INSERTING VALUE IN TABLE EMPLOYEE: SQL> insert into employee values(1,'mukul',30000,'west',28,'a',10); SQL> insert into employee values(2,'kritika',35000,'centre',30,'a',10); SQL> insert into employee values(3,'naveen',32000,'west',40,'',20); SQL> insert into employee values(4,'uday',38000,'north',38,'c',30); SQL> insert into employee values(5,'nupur',32000,'east',26,'',20); SQL> insert into employee values(6,'moksh',37000,'south',28,'b',10); SQL> insert into employee values(7,'shelly',36000,'north',26,'a',30);

INSERTING VALUE IN TABLE DEPARTMENT: SQL> insert into department values(10,'sales',25000,32000,1); SQL> insert into department values(20,'finance',30000,50000,5); SQL> insert into department values(30,'admin',25000,40000,7);

Simple select (question 2 to 3) izUk2% Table employee ds lHkh employees fd Details dks iznfkr fdft,A ans(2): output: NO -1 2 3 4 5 6 7 NAME ------mukul kritika naveen uday nupur moksh shelly SALARY -----30000 35000 32000 38000 32000 37000 36000 ZONE -----west centre west north east south north AGE GRADE -----28 a 30 a 40 38 c 26 28 b 26 a DEPT ---10 10 20 30 20 10 30 select * from employee;

izUk3% Table employee ds lHkh employees fd Salary, Zone, Grade dks iznfkr fdft,A ans(3): output: SALARY ---------30000 35000 32000 38000 32000 37000 36000 ZONE -------west centre west north east south north GRADE --------a a c b a select salary, zone, grade from employee;

conditional select using where clause (question 4 to 6) izUk4% Table employee ds lHkh employees fd Details iznfkr fdft, ftudh mez 30 lky ls de gksA ans(4): select * from employee where age < 30; output: NO --1 5 6 7 NAME --------mukul nupur moksh shelly SALARY ----------30000 32000 37000 36000 ZONE -------west east south north AGE GRADE DEPT --------------------28 a 10 26 20 28 b 10 26 a 30

izu 5% Table employee ds mu lHkh employees fd Details iznfkr fdft, tsk North Zone es dk;Z djrs gksA ans(5): output: NO -4 7 NAME -------uday shelly SALARY ZONE ----------------38000 north 36000 north AGE -----38 26 GRADE ---------c a DEPT ------30 30 select * from employee where zone = 'north';

izu 6% Table employee ds mu lHkh employees fd Details iznfkr fdft, tsk Department 10 es dk;Z djrs gksA ans(6): output: SALARY ----------30000 35000 37000 select salary from employee where dept = 10;

Using Null (question 7 to 8) izu 7 % Table employee ds mu lHkh employees fd Details iznfkr fdft, ftudh Grade Null gksA ans(7): output: select * from employee where grade is null;

NO ----3 5

NAME --------naveen nupur

SALARY ------------32000 32000

ZONE -------west east

AGE -----40 26

GRADE -----------

DEPT ------20 20

izu 8% Table employee ds mu lHkh employees fd Details iznfkr fdft, ftudh Grade Null ugh gksA ans(8): output: NO -1 2 4 6 7 NAME ------mukul kritika uday moksh shelly SALARY -----30000 35000 38000 37000 36000 ZONE -----west centre north south north AGE GRADE -----28 a 30 a 38 c 28 b 26 a DEPT ---10 10 30 10 30 select * from employee where grade is not null;

Using distinct clause (question 9) izu 9% Table employee ds mu lHkh zone ds uke iznfkr fdft, ftu zones ds uke flQZ ,d ckj gh fn;k x;k gksA ans(9): output: ZONE -----------------------------centre east north south west select distinct zone from employee;

Using Logical operators And , or , not (question 10 to 13) izu 10% Table employee ds mu lHkh employees fd Details iznfkr fdft, ftudh mez 30 lky ls de gksA ans(10): select * from employee where dept = 10 and age<30; output: NO -1 6 NAME ----mukul moksh SALARY ZONE AGE GRADE ----------- ------- ------ ---------30000 west 28 a 37000 south 28 b DEPT ------10 10

izu 11% Table employee ds mu lHkh employees ds uke rFkk salary iznfkr fdft, tsk u rks west zone es dke djrs gks vksj u centre zone es dke djrs gksA ans(11): select name,salary from employee where zone <> 'west' and zone <> 'centre'; output: NAME SALARY ------------------------------ ---------uday 38000 nupur 32000 moksh 37000 shelly 36000

izu 12% Table employee ds mu lHkh employees ds uke iznfkr fdft, tks fd department 20 ;k 30 es dke djrs gksA ans(12): select name from employee where dept = 20 or dept = 30; output: NAME -----------------------------naveen uday nupur shelly

izu 13% Table employee ds mu lHkh employees fd Details iznfkr fdft, ftudh salary 32000 ls 38000 ds chp gSA ans(13): select * from employee where salary between 32000 and 38000; output: NO -2 3 4 5 6 7 NAME ------kritika naveen uday nupur moksh shelly SALARY -----35000 32000 38000 32000 37000 36000 ZONE -----centre west north east south north AGE -----30 40 38 26 28 26 GRADE DEPT ------------a 10 20 c 30 20 b 10 a 30

Using In operator (question 14 to 16) izu 14% Table employee ds mu lHkh employees fd Details iznfkr fdft, tks Department 20 ;k 30 es dke djrs gSA (using in operator) ans(14): output: NAME -----------------------------naveen uday nupur shelly select name from employee where dept in (20,30);

izu 15% Table employee ds mu lHkh employees ds uke rFkk salary iznfkr fdft, tsk u rks west zone es dke djrs gks vksj u centre zone es dke djrs gksA (using in operator) ans(15): Output: NAME -----------------------------uday nupur moksh shelly SALARY ---------38000 32000 37000 36000 select name, salary from employee where zone not in('west', 'centre');

izu 16% Table employee ds mu lHkh employees fd Details iznfkr fdft, ftudh salary 32000 ls 38000 ds chp gSA (using between operator) ans(16): select * from employee where salary between 32000 and 38000; output: NO -2 3 4 5 NAME ------kritika naveen uday nupur SALARY ----------35000 32000 38000 32000 ZONE -----centre west north east AGE GRADE DEPT ------ ---------------30 a 10 40 20 38 c 30 26 20

6 7

moksh shelly

37000 36000

south north

28 26

b a

10 30

**************************************************************************************

Using Like operator (question 17 to 21) izu 17% Table employee ds mu lHkh employees ds name , salary rFkk age iznfkr fdft, ftu employee dk uke m ls izkjEHk gksrs gSA ans(17): select name, salary, age from employee where name like 'm%'; output: NAME SALARY AGE --------- ------------ -----mukul 30000 28 moksh 37000 28 ************************************************************************************** izu 18% Table employee ds mu lHkh employees ds name , salary rFkk age iznfkr fdft, ftu employee ds uke ds vUr es a gksA ans(18): select name, salary, age from employee1 where name like '%a'; output: NAME --------kritika SALARY -----------35000 AGE -----30

************************************************************************************** izu 19% Table employee ds mu lHkh employees ds name , salary rFkk age iznfkr fdft, ftu employee ds uke es a kCn vkrk gksA ans(19): select name,salary,age from employee1 where name like '%a%'; output: NAME -----------------------------kritika naveen uday SALARY ---------35000 32000 38000 AGE ---------30 40 38

izu 20% Table employee ds mu lHkh employees ds name , salary rFkk age iznfkr fdft, ftu employee ds uke es a kCn ugh vkrk gksA ans(20): select name, salary, age from employee where name not like '%a%';

NAME -----------------------------mukul nupur moksh shelly

SALARY ------------30000 32000 37000 36000

AGE ---------28 26 28 26

izu 21% Table employee ds mu lHkh employees ds name , salary rFkk age iznfkr fdft, ftu employee ds uke es nqljk kCn a vkrk gksA ans(21): output: NO -3 NAME ------naveen SALARY -----32000 ZONE -----west AGE --40 GRADE DEPT ------20 select * from employee1 where name like '_a%';

*************************************************************************************** Using aggregate functions (question 22 to 24) izu 22% Table employee ds lHkh employees fd salary dk sum rFkk average iznfkr fdft,A ans(22): output: TOTAL ---------240000 AVERAGE -------------34285.7143 select sum(salary) as total,avg(salary) as average from employee;

izu 23% Table employee ds Department 10 dks fn tkus okyh highest rFkk lowest salary dks iznfkr fdft,A ans(23): output: MAXSALARY ------------------37000 MINSALARY ----------------30000 select max(salary) as maxsalary, min(salary) as minsalary from employee where dept = 10;

izu 24% Table employee ds Department 10 es dke djus okys employees fd la[;k iznfkr fdft,A ans(24): Output: COUNT(NAME) select count(name) from employee where dept = 10;

--------------------3

Using order by clause (question 25 to 27) izu 25% Table employee ds lHkh employee fd detail mudh salary ds vkjksgh dze es iznfkr fdft,A ans(25): output: NO -----1 3 5 2 7 6 4 NAME --------mukul naveen nupur kritika shelly moksh uday SALARY -----------30000 32000 32000 35000 36000 37000 38000 ZONE -------west west east centre north south north AGE -----28 40 26 30 26 28 38 GRADE ---------a a a b c DEPT ------10 20 20 10 30 10 30 select * from employee order by salary;

izu 26% Table employee ds lHkh employee fd detail mudh salary ds vojksgh dze es iznfkr fdft,A ans(26): output: NO -----4 6 7 2 5 3 1 NAME --------uday moksh shelly kritika nupur naveen mukul SALARY -----------38000 37000 36000 35000 32000 32000 30000 ZONE -------north south north centre east west west AGE -----38 28 26 30 26 40 28 GRADE ---------c b a a a DEPT ------30 10 30 10 20 20 10 select * from employee order by salary;

Using group by clause (question 27) izu 27% Table employee ds gj es department es ekStqn employee fd dqy la[;k iznfkar fdft,A

ans(27): DEPT ---------10 20 30

select dept, count(*) as "total employee"from employee group by dept; total employee -------------3 2 2

Using UPDATE, DELETE, ALTER TABLE (question 28 to 27) izu 27% Table employee Eks ftu employee fd grade null gS mudh grade field es grade B insert djkb;sA ans(27): output: NO -1 2 3 4 5 6 7 NAME ------mukul kritika naveen uday nupur moksh shelly SALARY -----30000 35000 32000 38000 32000 37000 36000 ZONE -----west centre west north east south north AGE GRADE -----28 a 30 a 40 b 38 c 26 b 28 b 26 a DEPT ---10 10 20 30 20 10 30 update employee set grade = 'b' where grade is null;

izu 28% Table employee Eks mu lHkh employee ds record delete fdft, ftudk department 10 gSs rFkk salary 30000 ls de gksA ans(28): Output: delete from employee where grade = 'c' and salary <30000; 0 rows deleted.

izu 29% Table employee Eks ,d record blood group (blgrp) tksfM, ftldk datatype varchar gksA ans(29): output: NO ---NAME --------SALARY -----------ZONE -------AGE -----GRADE ---------DEPT ------BLGRP --------alter table employee add blgrp varchar(4);

CREATING VIEW: izu 30% ,d view west_zone create fdft, tks flQZ west zone es dk;Z djus okys employees ds records iznfkr djsA ans(30): OUTPUT: create view west_zone as select * from employee where zone = 'west'; View created.

izu 31% view west_ dk leiw.kZ Data iznfkr fdft,A ans(31): output: NO ----1 3 NAME --------mukul naveen SALARY -----------30000 32000 ZONE -------west west AGE ------28 40 GRADE ---------a b DEPT -------10 20 BLGR -------select * from west_zone;

******************************************************************************************* izu 32% ,d view west_zone create fdft, tks flQZ west zone es dk;Z djus okys employees ds records iznfkr djsA ans(32): select employee.no, employee.name, employee.dept, department.dname from employee full join department on employee.dept = department.dept order by employee.name; NAME --------kritika moksh mukul naveen nupur shelly uday DEPT -------10 10 10 20 20 30 30 DNAME ----------sales sales sales finance finance admin admin

NO ---2 6 1 3 5 7 4

Deletin and Drop in the tables & drop view: izu 33% view west_zone dks drop fdft,A ans(33) SQL> drop view amar;

OUTPUT:

View dropped.

izu 34% Table employee es ls og employees delete fdft, tks department 10 es dk;Z djrs gks A ans(34): delete from employee where dept = 10; output: 3 row deleted.

izu 35% Table employee rFkk Table department dks drop fdft,A ans(35): SQL> drop table employee; OUTPUT: Table dropped. SQL> drop table department; OUTPUT: Table dropped.