This document provides examples of SQL queries based on various tables. It includes 15 sections with sample tables and SQL queries to retrieve information from those tables. The queries cover basic selects, filters, sorting, grouping, aggregation and other SQL operations. The document is a good reference for learning different types of SQL queries that can be written on various table structures.
MS4 level being good citizen -imperative- (1) (1).pdf
SQL QUERIES FOR EMPLOYEE TABLE
1. 1) Answer the following queries based on the given table:EMP NO. ENAME
JOB
SALARY
DEPT.NO.
7680
SCOTT
MANAGER
20,000
10
7688
JACK
CLERK
5,000
20
7454
MACK
ANALYST
2,000
10
7210
BEN
MANAGER
20,000
30
7621
MARTIN
SALESMAN
15,000
40
a) Write a query to display the list of employee with salary.
Ans:- select ename ,sal
fromemp;
b) Write a query to display all the records of employee table.
Ans:- select empno,ename,job,sal , dept no
fromemp;
c) Write a query to display the name of employee along with designation &
salary.
Ans:- select ename,job,sal
fromemp;
d) Write a query to display the list of employees who get salary more than
10,000.
Ans:- select ename
fromemp
wheresal> 10,000;
e) Write a query to display all records of dept no. 10.
Ans:- select* from emp where dept no=10;
2. 2) Table 1.2 shows the records of a shop named customers. Give the query to
the questions asked based on the table.
CUST NO.
NAME
AMOUNT(RS/-) PRODUCT
101
ALKA
20
MAGGI
509
SURESH
467
KISSAN JAM
01
MAHIMA
945
PULSES
240
DECK
1,000
RICE
109
HEENA
45
MACRONI
a) Write a query to display all the records of purchase of pulses product.
Ans:- select* from customer
where product=`pulses’;
b) Write a query to display the total sales(in terms of rupee). Total
sales=amount * 20
Ans:- select amount * 20 “total sales”
from customer;
c) Write a query to display the name of customers with product whose
name starts from H and ends with A.
Ans:- select name,product
from customer
where namelike`H%A’ ;
d) Write a query to display all the records of cust no.109
Ans:- select* from customer
wherecust no=109 ;
3. 3) Write sql commands for the following on the basis of the table student.
STUDENT
NO.
CLASS
NAME
GAME
GRADE 1
SUPW
10
7
SAMEER
CRICKET
B
PHOTOGRAPHY A
11
8
SUJIT
TENNIS
A
GARDENING
12
7
KAMAL
SWIMMING
B
PHOTOGRAPHY B
13
7
VEENA
TENNIS
C
COOKING
A
14
9
ARCHANA
BASKET BALL
A
LITERATURE
A
15
10
ARPIT
CRICKET
A
GARDENING
C
GRADE 2
C
a) Display the name of the students who are getting a grade `c’ in either game
or supw.
Ans:- select name from student
whereGrade 1 =`C’ or Grade 2 = `C’ ;
b) Display the different games offered in the school.
Ans:- select distinct (Game)
from student;
c) Display the SUPW taken up by the students whose name starts with `A’.
Ans:- select SUPW from student
where name LIKE `A%’;
4. 4) Write sql commands for the following on the basis of the given table CLUB.
COACH- ID
COACH NAME
AGE
SPOERTS
DATE OF APP
PAY
SEX
1
KUKREJA
34
KARATE
27/3/1996
1000
M
2
RAVINA
34
KARATE
20/01/1998
1200
F
3
KARAN
34
SQUASH
19/02/1998
2000
M
4
TARUN
33
BASKETBALL
01/01/1998
1500
M
5
ZUBIN
36
SWIMMING
12/01/1998
750
M
6
KETAKI
36
SWIMMING
24/02/1998
800
F
7
ANKITA
39
SQUASH
20/02/1998
2200
F
8
ZAREEN
37
KARATE
22.02/1998
1100
F
9
KUSH
41
SWIMMING
13/01/1998
900
M
10
SHAILYA
37
BASKETBALL
19/02/1998
1700
M
a) To show all the information about the swimming coaches in the club.
Ans:- select* from club
where sports=`SWIMMING’;
b) To list the names of all coaches with their date of appointment
(date of app) in descending order.
Ans:- select COACH NAME, DATE OF APP from club
order by DATE OF APP desc;
c) To display a report, showing coachname, pay, age & bonus (15% of pay)
for all the coaches.
Ans:- select COACHNAME,PAY,AGE,P*15 “BONUS”
from club;
5. 5) Write sql commands for the following on the basis of the given table
STUDENT 1.
NO NAME
STIPEND
STREAM
AVG. MARKS GRADE CLASS
1
KARAN
400.00
MEDICAL
78.5
B
12B
2
DIVAKAR
450.00
COMMERCE
89.2
A
11C
3
DIVYA
300.00
COMMERCE
68.6
C
12C
4
ARUN
350.00
HUMANITIES
73.1
B
12C
5
SABINA
500.00
NON-MEDICAL
90.6
A
11A
6
JOHN
400.00
MEDICAL
75.4
B
12B
7
ROBERT
250.00
HUMANITIES
64.4
C
11A
8
RUBINA
450.00
NON-MEDICAL
88.5
A
12A
9
VIKAS
500.00
NON-MEDICAL
92.0
A
12A
10
MOHAN
300.00
COMMERCE
67.5
C
12C
a) select all the numerical stream students from STUDENT 1.
Ans:- select name,stream from STUDENT 1
where stream= `non-medical’;
b) list all name os those students who are in class 12 sorted by stepend.
Ans:- select name from STUDENT 1
where class LIKE `12-’
Order By stipend;
c) list all students stored by average marks in descending order.
Ans:- select name from STUDENT 1
Order by avg. marks DESC;
d) display a report, listing name, stipend,stream& amount of stipend received
in a year assuming that the stipend is paid every month.
Ans:- select name,stipend * 12,stream from STUDENT1 ;
6. 6) Write sql commands for the following on the basis of given table LIBRARY.
NO. TITLE
AUTHOR
TYPE
PUB
QUANTITY PRICE
1
DATA STRUCTURE
LIPSCHUTZ
DS
MC GRAW
4
217
2
COMPUTER STUDIES
FRENCH
FND
GALGOTIA
2
75
3
ADVANCE PASCAL
SCHILDT
PROG
MC GRAW
4
350
4
DBASE DUMMIES
PALMER
DBMS
PUSTALE M
5
130
5
MASTERING C++
GWEWICH
PROG
BPB
3
295
6
GUIDE NETWORK
FREED
NET
Z PRESS
3
200
7
MASTERING FOXPRO
SEIGAL
BDMS
BPB
2
135
8
DOS GUIDE
MORTON
OS
PHI
3
175
9
BASIC FOR BEGINNERS
MORTON
PROG
BPB
3
40
10
MASTERING WINDOWS
COWARD
OS
BPB
1
225
a) Select all the PROG type published by BPB from library.
Ans:- select* from library
where type= `PROG’AND PUB= `BPB’ ;
b) Display a list of all books with price more than 130&sDrted by qty.
Ans:- select* from library
where price>130
Order by qty;
c) Display all the books sorted by price in ascending order.
Ans:- select* from library
Order by price ASCE;
7. 7) Write
NO. TITLE
sql commands for the following on the basis of the given table MOV.
TYPE
RATING STARS
QTY.
PRICE
1
GONE WITH THE WIND
DRAMA
G
GABLE
4
39.95
2
FRIDAY THE 13th
HORROW
R
JASON
2
69.95
3
TOP GUN
DRAMA
PG
CRUISE
7
49.95
4
SPLASH
COMEDY
PG13
HANKS
3
29.95
5
INDEPENDENT DAY
DRAMA
R
TURNER
3
19.95
6
RISKY BUSINESS
COMEDY
R
CRUISE
2
44.95
7
COCOON
SCIFI
PG
AMECHE
2
31.95
8
CROCODILE DUNDEE
COMEDY
PG13
HARRIS
2
69.95
9
101 DALMATIANS
COMEDY
G
PATTINSON
3
59.95
10
TOOTSIE
COMEDY
PG
HOFFMAN
1
29.95
a) Display a list of all movies with price over 20 & sorted by price
Ans :- select Title , price from MOV
where price >20
Order by price;
b) Display all the movies sorted by QTYin decreasing order
Ans :- select *from MOV
Order by qty DESC;
c) Display a report listing a movie number , current value & replacement
value for each movie in the given table. Calculate the replacement value
for all the movies as aty*price*1.15.
Ans:- select no., price,qty.*price*1.15
from MOV
Group by no;
8. 8) Write
sql commands for the following on the basis of the given table
GRADUATE.
S.NO. NAME
STIPEND SUBJECT
AVERAGE RANK
1
KARAN
400
PHYSICS
68
1
2
DIVAKAR
450
COMPUTER SCIENCE
68
1
3
DIVYA
300
CHEMISTRY
62
2
4
ARUN
350
PHYSICS
63
1
5
SABINA
500
MATHEMATICS
70
1
6
JPHN
400
CHEMISTRY
55
2
7
ROBERT
250
PHYSICS
64
1
8
RUBINA
450
MATHEMATICS
68
1
9
VIKAS
500
COMPUTER SCIENCE
62
1
10
MOHAN
300
MATHEMATICS
57
2
a) List the names of those students who have obtained rank 1 by NAME.
Ans:- select NAME from graduate
where rank=`1’
Order by NAME;
b) Display a report listing NAME,STIPEND,SUBJECT& amount of stipend
received in a year assuming that the STIPEND is paid every month.
Ans:- select NAME, STIPEND,SUBJECT,STIPEND * 12
from GRADUATE;
9. 9) Write sql commands for the following on the basis of the given table
Teacher.
NO. NAME
AGE DEPARTMENT
DATE OF JOIN
SALARY
SEX
1
JUGAL
34
COMPUTER
10/01/97
12000
M
2
SHARMILA
31
HISTORY
24/03/98
20000
F
3
SANDEP
32
MATHS
12/12/96
30000
M
4
SANGEETA
35
HISTORY
01/07/99
40000
F
5
RAKESH
42
MATHS
05/09/97
25000
M
6
SHYAM
50
HISTORY
27/06/98
30000
M
7
SHIV OM
44
COMPUTER
25/02/97
21000
M
8
SHALAKHA
33
MATHS
31/07/97
20000
F
a) To show all information about the teacher of history department.
Ans:- select* from teacher
where Department=`HISTORY’;
b) To list the names of female teachers who are in mathsdepartment.
Ans:- select name from teacher
where department=`MATHS’ AND sex=`F’;
c) To list the names of all teachers with their date of joining in ascending
order.
Ans:- select Name, Date of join
from teacher
Order by Date of join ASC;
10. 10)
Write a query to create table student having following
specifications:Column name type
size
constraint
Sname
char
40
Cls
int
>6
Marks
number
2,2
unique
default
Ans:-Create table student
( sid number primary key,
sname char(40) unique,
clsint check(cls.6),
marks number(2,2) default=12);
11)
Write a query to create table customer having following
specifications:Column name type
size
constraint
Cno.
Number
3
Name
varchar
10
Amount
Float
-
>5000
comn
Number
-
Default=1001
A ns:- Create table customer
( cno number(3) primary key,
name varchar(10),
amount float check(amount>5000),
comn number default=1001);
Primary
11. 12)
Write a query to create table ITEM
Column name
type
Item no.
Number
Item name
Varchar
40
Amount
Number
4,2
Qty.
number
Ans:- create table Item
(
Item no number primary,
Inamevarchar(40),
Amount number(4,2),
Qty number check (qty>100)
);
size
constraint
primary
>100
12. 13)
Write queries based on the following table PET.
NAME
OWNER
SPECIES
SEX
BIRTH
DEATH
FLUFFY
HAROLD
CAT
F
4/2/1993
NULL
CLAWS
GWEN
CAT
M
17/3/1994
NULL
BUFFY
HAROLD
DOG
F
13/5/1989
NULL
FANG
BENNY
DOG
M
27/8/1990
NULL
BROWSER
DIANE
DOG
M
31/8/1979
29/7/1995
EHIRPY
GWEN
BIRD
F
11/9/1998
NULL
WHISTLER
GWEN
BIRD
NULL
9/12/1997
NULL
SLIM
BENNY
SNAKE
M
29/4/1996
NULL
PUFFBALL
DIANE
HAMSTER
F
30/3/199
NULL
a) Display all the details from PET table for species cat/dog having gender
(sex )as male(`M’).
Ans:-select* from PET
where (species=`cat’||specie=`dog’) &&
sex=`m’;
b) Display all details of pet of species bird,snake or hamster from table PET
Ans:- select* from PET
where species in (`bird’,`snake’,`hamster’);
c) Display the names of pets who are no more.
Ans:- select name from PET
where death IS NOT NULL;
13. 14)
Consider the table EXAM given.Write commands in mysql.
NO NAME
STIPEND
SUBJECT
AVG.
DIVISION
1
KARAN
400
ENGLISH
68
FIRST
2
AMAN
680
MATHS
72
FIRST
3
JAVED
500
ACOUNTS
67
FIRST
4
BISHAKH
200
INFORMATICS
55
SECOND
5
SUGANDHA
400
HISTORY
35
THIRD
6
SUPARNA
550
GEOGRAPHY
45
THIRD
a) To list the names of those students, who have obtained division as FIRST
in the ascending order of NAME.
b)
Ans:- select name,division from EXAM
where division=`FIRST’
Order by NAME ASC;
c) To display a report listing name, subject, annual stipend received
assuming that the stipend column has monthly stipend.
Ans: select name, stpend,subject * 12 from EXAM;
d) To count the number of students who have either accounts or
informatics as subject.
Ans:- select name from EXAM
where subject=`Accounts’ or subject=`Informatics’;
14. 15)
Write sql commands based on the given table EMPL.
EMP
NO
ENAME
JOB
MGR.
HIREDATE
SAL
COMM.
8369
SMITH
CLERK
8902
18/12/1990
800
NULL
8499
ANYA
SALESMAN
8698
4/2/1990
1600
300
8521
SETH
MANAGER
8698
18/7/1991
2000
500
8566
MAHADEVAN SALESMAN
8839
30/4/1990
400
NULL
8698
MOMIN
CLERK
NULL
28/6/1992
7800
1400
8888
BEENA
ANALYST
8854
4/8/1990
2000
NULL
8900
AMIR
CLERK
6478
4/4/1991
1100
NULL
8844
KULDEEP
PRESIDENT
8522
8/9/1999
5000
0.00
a) Cunt number of records in table employee.
Ans:-select count(*) “Total”
b)
c)
d)
e)
from EMPL;
Count number of jobs in table EMPL.
Ans:- select count(job) “job count “
from EMPL;
How many distinct jobs are listed in the table EMPL.
Ans:- select count(distinct job) “distict jobs”
from EMPL;
Display max. salary from the table EMPL.
Ans:- select max(sal) “maximum salary”
from EMPL;
Display the joining date of senior most employee.
Ans:- select MIN (hiredate) “minimum hiredate”
From EMPL;