0% found this document useful (0 votes)
108 views11 pages

SQL Exercise

The document provides SQL exercises and queries with sample tables. It includes queries to display client, product and consignor details based on various conditions. It also shows queries to order results and group data.

Uploaded by

ashvinvkambar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
108 views11 pages

SQL Exercise

The document provides SQL exercises and queries with sample tables. It includes queries to display client, product and consignor details based on various conditions. It also shows queries to order results and group data.

Uploaded by

ashvinvkambar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

SQL EXERCISES

SQL EXERCISE 1

TABLE NAME : PRODUCT

P_ID PRIDUCTNAME MANUFACTURER PRICE


TP01 TALCUM POWDER LAK 40
FW01 FACE WASH ABC 45
BS01 BATH SOAP ABC 55
SH06 SHAMPOO XYZ 120
FW12 FACE WASH XYZ 95
TABLE NAME : CLIENT

C_ID CLIENTNAME CITY P_ID


01 COSMETIC SHOP DELHI FW05
06 TOTAL HEALTH MUMBAI BS01
12 LIVE LIFE DELHI SHO6
15 PRETTY WOMAN DELHI FW12
16 DREAMS BANGALORE TP01

Write the Queries for the following :


1. To display the details of clients belonging to Delhi
2. To display the details of products whose price is between 50 and 100
3. To display the client name , city from the table ‘Client’ and product name , price from table ‘ product’ with
corresponding matching P_ID
4. To increase the price of all products by 10
Queries:
1. SELECT * FROM CLIENT WHERE CITY =’ DELHI’;
2. SELECT * FROM PRODUCT WHERE PRICE BETWEEN 50 AND 100
3. SELECT CLIENTNAME, CITY,PRODUCTNAME, PRICE FROM CLIENT , PRODUCT WHERE CLIENT.P_ID =PRODUCT.P_ID;
4. UPDATE PRODUCT SET PRICE=PRICE+10;

Output:
1. SELECT * FROM CLIENT WHERE CITY =’DELHI’;
C_ID CLIENTNAME CITY P_ID
01 COSMETIC SHOP DELHI FW05
12 LIVE LIFE DELHI SHO6
15 PRETTY WOMAN DELHI FW12

2. SELECT * FROM PRODUCT WHERE PRICE BETWEEN 50 AND 100;


P_ID PRIDUCTNAME MANUFACTURE PRICE
R
BS01 BATH SOAP ABC 55
SH06 SHAMPOO XYZ 120
FW12 FACE WASH XYZ 95

3. SELECT CLIENTNAME, CITY, PRICE FROM CLIENT , PRODUCT WHERE CLIENT.P_ID =PRODUCT.P_ID;

CLIENTNAME CITY PRODUCTNAME PRICE


TOTAL HEALTH MUMBAI BATH SOAP 55
LIVE LIFE DELHI SHAMPOO 120
PRETTY DELHI FACE WASH 95
WOMAN
DREAMS BANGALORE TALCUM POWDER 40

4. UPDATE PRODUCT SET PRICE=PRICE+10;


5 ROWS AFFECTED IN 0.02 SEC
AFTER UPDATE WE CAN SEE THE CONTENT OF TABLE BY
SELECT * FROM PRODUCT;
SO THAT OUTPUT IS
P_ID PRIDUCTNAME MANUFACTURER PRICE
TP01 TALCUM POWDER LAK 50
FW01 FACE WASH ABC 55
BS01 BATH SOAP ABC 65
SH06 SHAMPOO XYZ 130
FW12 FACE WASH XYZ 105
SQL EXERCISE 2
TABLE : CONSIGNOR
CNOR_ID CNORNAME CNORADDRESS CITY
ND01 R SINGHAL 24, ABC ENLAVE NEW DELHI
ND02 AMIT KUMAR 123, PALM AVENUE NEW DELHI
MU15 R KOHLI 51 A , SOUTH STREET MUMBAI
MU50 S KAUR 27-K WERTEND MUMBAI

TABLE : CONSIGNEE
CNEEID CNORID CNEENAME ADDRESS CNEECITY
MU05 ND01 RAHUL KISHORE 5, PARK AVENUE MUMABI
ND08 ND02 P.DHINGRA 16/5, MOORE EOC NEW DELHI
K019 MU15 A P ROY 2/A CENTRAL AV KOLKATA
MU32 NDO2 S.MITTAL 0-245, ABCD MUMBAI
ND48 MU05 B.P.MITTAK 13, BLOCK D AV NEW DELHI

Write Queries for the following:


1. To display the names of all consignors from Mumbai
2. To display the CNEEID , CNORNAME, CNORADDRESS, CNEENAME, CNEEADDRESS FOR EVERY CONSIGNEE
3. To display consignee details in ascending order of cneename
4. To display number of consignors from each city
Queries:
1. SELECT CNORNAME FROM CONSIGNOR WHERE CITY =’MUMBAI’;
2. SELECT CNEEID, CNORNAME, CNORADDRESS, CNEENAME, CNEEADDRESS FROM CONSIGNOR , CONSIGNEE
WHERE CONSIGNOR.CNORID=CONSIGNEE.CNORID;
3. SELECT * FROM CONSIGNEE ORDER BY CNEENAME ASC;
4. SELECT CNEECITY , COUNT(CNEECITY) FROM CONSIGNEE GROUP BY CNEECITY;

Output:
1. SELECT CNORNAME FROM CONSIGNOR WHERE CITY =’MUMBAI’;

CNORNAME
R KOHLI
S KAUR

2. SELECT CNEEID, CNORNAME, CNORADDRESS, CNEENAME, CNEEADDRESS FROM CONSIGNOR , CONSIGNEE


WHERE CONSIGNOR.CNORID=CONSIGNEE.CNORID;
CNEEID CNORNAME CNORADDRESS CNEENAME ADDRESS
MU05 R SINGHAL 24, ABC ENLAVE RAHUL KISHORE 5, PARK AVENUE
ND08 AMIT KUMAR 123, PALM AVENUE P.DHINGRA 16/5, MOORE EOC
K019 R KOHLI 51 A , SOUTH STREET A P ROY 2/A CENTRAL AV
MU32 S KAUR 27-K WERTEND S.MITTAL 0-245, ABCD

3. SELECT * FROM CONSIGNEE ORDER BY CNEENAME ASC;


CNEEID CNORID CNEENAME ADDRESS CNEECITY
K019 MU15 A P ROY 2/A CENTRAL AV KOLKATA
ND48 MU05 B.P.MITTAK 13, BLOCK D AV NEW DELHI
ND08 ND02 P.DHINGRA 16/5, MOORE EOC NEW DELHI
MU05 ND01 RAHUL KISHORE 5, PARK AVENUE MUMABI
MU32 NDO2 S.MITTAL 0-245, ABCD MUMBAI

4. SELECT CNEECITY , COUNT(CNEECITY) FROM CONSIGNEE GROUP BY CNEECITY;

CNEECITY COUNT(CNEECITY)
MUMABI 2
NEW DELHI 2
KOLKATA 1

SQL EXERCISE 3:
TABLE :STOCK
Itemno Item Dcode Qty Unitprice Stockdate
5005 Ball Pen 0.5 102 100 16 2010-03-31,
5003 Ball pen 0.25 102 150 20 2020-01-01
5002 Gel Pen Premium 101 125 14 2010-02-14
5006 Gel Pen classic 101 200 22 2009-01-01
5001 Eraser Small 102 210 5 2009-03-19
5004 Eraser Big 102 60 10 2009-12-12
5009 Sharpener Classic 103 160 8 2009-01-23

Table: Dealers
Dcode Dname
101 Reliable Stationers
103 Classic Plastics
102 Clear Deals

Write Queries for the following:


1. To display details of all items in the stock table in ascending order of stockdate
2. To display Itemno and itemname of those items from stock table whose unitprice is more than Rupees 10
3. To display the details of those items whose dealer code is 102 or quantity in stock is more than 100 from
the table stock
4. To display maximum unit price of items for each dealer individually as per dcode from the table stock
Queries :
1.SELECT * FROM Stock ORDER BY Stockdate;
2.SELECT Itemno, Item FROM Stock WHERE Unitprice >10;
3.SELECT * FROM Stock WHERE Dcode=102 OR Qty>100;
4.SELECT Dcode, MAX(Unitprice) FROM Stock GROUP BY Dcode;

Output:
1.SELECT * FROM Stock ORDER BY Stockdate;
Itemno Item Dcode Qty Unitprice Stockdate
5006 Gel Pen classic 101 200 22 2009-01-01
5009 Sharpener Classic 103 160 8 2009-01-23
5001 Eraser Small 102 210 5 2009-03-19
5004 Eraser Big 102 60 10 2009-12-12
5003 Ball pen 0.25 102 150 20 2010-01-01
5002 Gel Pen Premium 101 125 14 2020-02-14
5005 Ball Pen 0.5 102 100 16 2010-03-31

2.SELECT Itemno, Item FROM Stock WHERE Unitprice >10;


Itemno Item
5005 Ball Pen 0.5
5003 Ball pen 0.25
5002 Gel Pen Premium
5006 Gel Pen classic

3.SELECT * FROM Stock WHERE Dcode=102 OR Qty>100;


Itemno Item Dcode Qty Unitprice Stockdate
5005 Ball Pen 0.5 102 100 16 2010-03-31
5003 Ball pen 0.25 102 150 20 2020-01-01
5002 Gel Pen Premium 101 125 14 2010-02-14
5006 Gel Pen classic 101 200 22 2010-01-01
5001 Eraser Small 102 210 5 2010-03-19
5004 Eraser Big 102 60 10 2009-12-12
5009 Sharpener Classic 103 160 8 2009-01-23

4.SELECT Dcode, MAX(Unitprice) FROM Stock GROUP BY Dcode;


Dcode Unitprice
102 20
101 22
103 8

SQL EXERCISE 4:
TABLE :Employees
Empid Firstname Lastname Address City
010 Ravi Kumar Raj nagar GZB
105 Harry Waltor Gandhi nagar GZB
152 Sam Tones 33 elm st. Paris
215 Sarah Ackerman 440 u.s. 10 Upton
244 Manila Senupta 24 friends street New delhi
300 Robert Samuel 9 fifth cross Washington
335 Ritu Tondon Shastri nagar GZB
400 Rachel Lee 121 harrison st. New yark
441 Peter Thompson 11 red road Paris

TABLE : EmpSalary
Empid Salary Benefits Designation

010 75000 15000 Manager

105 65000 15000 Manager

152 80000 25000 Director

215 75000 12500 Manager

244 50000 12000 Clerk

300 45000 10000 Clerk

335 40000 10000 Clerk

400 32000 7500 Salesman

441 28000 7500 Salesman

Write Queries for the following:


1. To show firstname, lastname, address and city of all employees living in Paris
2. To display the content of Employees table in descending order of Firstname
3. To display the firstname, lastname and total salary of all managers from the tables Employees and
Empsalary where total salary is calculated as Salary+benefits
4. To display the maximum salary among managers and clerks from the table Empsalary
Queries:
1. SELECT Firstname, Lastname, Address , City FROM Employees WHERE City=’Paris’;
2. SELECT * FROM Employees ORDER BY Firstname DESC;
3. SELECT Firstname, Lastname, Salary+Benefits as ‘Total Salary’ FROM Employees, Empsalary where
Employees.Empid=Empsalary.Empid and Designation=’Manager’;
4. SELECT Desgination, MAX(Salary) FROM Empsalary GROUP by Designation;
Output :
1. SELECT Firstname, Lastname, Address , City FROM Employees WHERE City=’Paris’;
Firstname Lastname Address City
Sam Tones 33 elm st. Paris
Peter Thompson 11 red road Paris

2. SELECT * FROM Employees ORDER BY Firstname DESC;


Empid Firstname Lastname Address City
105 Harry Waltor Gandhi nagar GZB
244 Manila Senupta 24 friends street New delhi
441 Peter Thompson 11 red road Paris
400 Rachel Lee 121 harrison st. New yark
010 Ravi Kumar Raj nagar GZB
335 Ritu Tondon Shastri nagar GZB
300 Robert Samuel 9 fifth cross Washington
152 Sam Tones 33 elm st. Paris
215 Sarah Ackerman 440 u.s. 10 Upton

3. SELECT Firstname, Lastname, Salary+Benefits as ‘Total Salary’ FROM Employees, Empsalary where
Employees.Empid=Empsalary.Empid and Designation=’Manager’;
Firstname Lastname Salary Benefits Total Salary
Ravi Kumar 75000 15000 90000
Harry Waltor 65000 15000 80000
Sarah Ackerman 75000 12500 87500
4. SELECT Desgination, MAX(Salary) FROM Empsalary GROUP by Designation;
Designation MAX(Salary)
Manager 75000
Director 80000
Clerk 50000
Salesman 32000

SQL EXERCISE 5:

Table : Books
Book_id Book_name Author_name Publishers Price Type Qty
C0001 Fast cook Lata kapoor EPB 355 Cookery 5
F0001 The tears William First publ. 650 Fiction 20
hopkins
T0001 My frist c++ Brain & EPB 350 Text 10
brooke
T0002 C++ brainworks A w rossaine TDH 350 Text 15
F0002 Thunderbolts Anna roberts First publ. 750 Fiction 50

TABLE : Issued
Book_id Quantity_issued
T0001 4
C0001 5
F0001 2

Write Queries for the following:


1. To show Book name, Author name and price of books of First Publ. Publishers
2. To display the names and price from books in ascending order of their price.
3. To increase the price of all books of EPB publishers by 50
4. To display the Book _id, Book_name and quantity_issued for all books which have been issued.
Queries:
1. SELECT Book_name, Author_name, Price FROM Books WHERE Publishers=’Firt Publ.’;
2. SELECT BOOK_name, Price FROM Books ORDER BY Price;
3. UPDATE BOOKS SET Price=Price+50 WHERE Publishers =’EPB’;
4. SELECT Books.Book_id, Book_name, Quantity_issued FROM Books, Issued
WHERE Books.Book_id=Issued.Book_id;

Output:
1. SELECT Book_name, Author_name, Price FROM Books WHERE Publishers=’Firt Publ.’;

Book_name Author_name
The tears William hopkins
Thunderbolts Anna roberts

2. SELECT BOOK_name, Price FROM Books ORDER BY Price;


Book_name Price
My frist c++ 350
C++ brainworks 350
Fast cook 355
The tears 650
Thunderbolts 750

3. UPDATE BOOKS SET Price=Price+50 WHERE Publishers =’EPB’;


After this Command, View the table to see the result by
SELECT * FROM Books;
Book_id Book_name Author_name Publishers Price Type Qty
C0001 Fast cook Lata kapoor EPB 405 Cookery 5
F0001 The tears William hopkins First publ. 650 Fiction 20
T0001 My frist c++ Brain & brooke EPB 400 Text 10

T0002 C++ brainworks A w rossaine TDH 350 Text 15


F0002 Thunderbolts Anna roberts First publ. 750 Fiction 50

4. SELECT Books.Book_id, Book_name, Quantity_issued FROM Books, Issued


WHERE Books.Book_id=Issued.Book_id;

Book_id Book_name Quantity_issued


C0001 Fast cook 5
F0001 The tears 2
T0001 My frist c++ 4

Python and database connectivity programs


1. Kabir wants to write a program in Python to insert the following
record in the table named Student in MYSQL database, SCHOOL:
 rno(Roll number )- integer
 name(Name) - string
 DOB (Date of birth) – Date
 Fee – float
Note the following to establish connectivity between Python and MySQL:
 Username - root
Password – tiger
 Host – localhost

The values of fields rno, name, DOB and fee has to be accepted from the user. Help
Kabir to write the program in Python.

2. Raj wants to write a program in Python to deletes the records with Ecode as
“E101” or “ E105” from the table named employee in MYSQL database,
EMP:
 Ecode - String
 Ename - string
 Sal- Integer
 City – String

Note the following to establish connectivity between Python and MySQL:


 Username - root
Password – root
 Host – localhost
The details(Ecode, Ename, Sal City) are attributes of
the table.

Help Raj to write the program in Python.

3. Hemanth wants to write a program in Python to read the following records from
the table employee and displays only those records who have employees coming
from city “Delhi”.

 Ecode - String
 Ename - string
 Sal- Integer
 City – String

Note the following to establish connectivity between Python and MySQL:


 Username - root
Password – root
 Host – localhost
 Database - EMP
The details(Ecode, Ename, Sal City) are attributes of
the table.

Help Hemanth to write the program in Python.

4. Srilakshmi wants to write a program in Python to increase the marks by 5 for all 12
std students in the table Midterm.
RollNo – integer
Name – string
Clas – integer
Marks – integer
Note the following to establish connectivity between Python and
MYSQL:
 Username is root
 Password is tiger
 The table exists in a MYSQL database named school.

You might also like