SQL_Practice1
SQL_Practice1
Link: https://www.w3resource.com/sql-exercises/
"Data Analytics Using Power BI Live Workshop "
ADITI GUPTA
Query 1
•Display name and commission of all the salesmen.
Query 1
•Display name and commission for all the
salesmen.
name commission
James Hoog 0.15
Nail Knite 0.13
Pit Alex 0.11
Mc Lyon 0.14
Paul Adam 0.13
Lauson Hen 0.12
SELECTname, commission
FROM salesman;
Query 2
•Retrieve salesman id of all salesmen from orders table without any
repeats.
Query 2
•Retrieve salesman id of all
salesmen from orders table
without any repeats.
salesman_id
5002 5003
5006 5001
5005 5007
SELECT DISTINCTsalesman_id
FROM orders;
Query 3
•Display names and city of salesman, who belongs to the city of Paris.
Query 3
• Display names and city of
salesman, who belongs to
the city of Paris.
name city
Nail Knite Paris
Mc Lyon Paris
SELECTname,city
FROM salesman
WHERE city='Paris';
Query 4
•Display all the information for those customers with a
grade of 200.
SELECT*
FROM customer
WHERE grade = 200;
Query 5
• Display the order number, order
date and the purchase amount for
order(s) which will be delivered by
thesalesmanwithID5001.
winner
Pablo Neruda
SELECTwinner
FROM nobel_win
WHERE year = 1971
AND subject = 'Literature';
Query 7
• Show all the details of the winners with first name Louis.
SELECT*
FROM nobel_win
WHERE winner LIKE'Louis%';
Query 8
• Show all the winners in Physics for 1970 together with the winner of Economics for 1971.
SELECT*
FROM nobel_win
WHERE year = 1970
AND subject NOT IN ('Physiology','Economics');
Query 10
• Find all the details of the Nobel winners for the subject not started with the letter 'P' and arranged
the list as the most recent comes first, then by name in order.
pro_name pro_price
ZIP drive 250.00
Mouse 250.00
SELECTpro_name, pro_price
FROM item_mast
WHERE pro_price= (SELECT MIN(pro_price )
FROM item_mast);
Query 12 (table: customer)
• Display all the customers, who are either belongs to the city New York or not had a grade above 100.
SELECT*
FROM customer
WHERE city = 'New York' OR NOT grade > 100;
Query 13 (table: salesman)
• Find those salesmen with all information who gets the commission within a range of 0.12 and 0.14.
SELECT*
FROM customer
WHERE cust_name LIKE'%n';
Query 15 (table: salesmen)
• Find those salesmen with all information whose name containing the 1st character is 'N' and the 4th
character is 'l' and rests may be any character.
SELECT*
FROM salesman
WHERE name LIKE‘N__l%';
Query 16 (table: customer)
• Find that customer with all information who does not get any grade except NULL.
SELECT*
FROM customer
WHERE grade ISNULL;
Query 17 (table: orders)
• Find the total purchase amount of all orders.
SELECTSUM(purch_amt)
FROM orders;
Query 18 (table: orders)
• Find the number of salesman currently listing for
all of their customers.
SELECT COUNT(salesman_id)
FROM orders;
SELECTsalesman_id, MAX(purch_amt)
FROM orders
WHERE ord_date= '2012-08-17'
GROUP BY salesman_id;
Query 23 (table: orders)
• Find the highest purchase amount with their customer ID
and order date, for only those customers who have the
highest purchase amount in a day is more than 2000.
SELECT COUNT(*)
FROM orders
WHERE ord_date= '2012-08-17';
SQL Practice 2
Multiple tables Joins Nested Queries
Link: https://www.w3resource.com/sql-exercises/
Order of SQL Statement
Query 1
•Find the name and city of those customers
and salesmen who lives in the same city.
cust_name name city
Nick Rimando James Hoog New York
Brad Davis James Hoog New York
Julian Green Pit Alex London
Fabian Johnson Mc Lyon Paris
Fabian Johnson Nail Knite Paris
Brad Guzan Pit Alex London
SELECTC.cust_nameS.name S.city
FROM salesman ASS customer ASC
WHERE S.city= C.city
Query 2
• Find the names of all customers along
with the salesmen who works for them.
cust_name name
Nick Rimando
James Hoog
Brad Davis
James Hoog
Graham Zusi
Julian Green
Nail Knite
Fabian Johnson Nail Knite
Geoff Cameron Mc Lyon
Jozy Altidor Lauson Hen
Brad Guzan Paul Adam
Pit Alex
SELECTcustomer.cust_namesalesman.name
FROM customer salesman
WHERE salesman.salesman_id= customer.salesman_id;
Query 3
• Display all those orders by the customers not located
in the same cities where their salesmen live.
customr
ord_no cust_name _id salesman_id
70004 Geoff Cameron 3009 5003 5003
70003 Geoff Cameron 3009 5007 5002
70011 Jozy Altidor 3003 5002 5002
70001 Graham Zusi 3005
70007 Graham Zusi 3005
70012 Julian Green 3008
SELECT*
FROM orders
WHERE salesman_id=
(SELECTsalesman_id
FROM salesman
WHERE name = 'Paul Adam');
SELECT *
FROM orders
WHERE purch_amt >
(SELECTAVG(purch_amt)
FROMorders
WHEREord_date= '2012-10-10');
SELECT*
FROM orders
WHERE salesman_idIN
(SELECTsalesman_id
FROM salesman
WHERE city ='Paris');
SELECTsalesman_id, name
FROM salesman ASa
WHERE 1 <
(SELECTCOUNT(*)
FROM customer ASc
WHERE c.salesman_id= a.salesman_id);
SELECT*
FROM salesman
WHERE salesman_id IN
(
SELECT DISTINCTsalesman_id
FROM customer a
WHERE NOT EXISTS(
SELECT*FROM customer b
WHERE a.salesman_id= b.salesman_id
AND a.cust_name<> b.cust_name));
Query 9: Equivalent Queries
Write a query to find all the salesmen who salesman_id name city commission
worked for only one customer. 5005 Pit Alex London 0.11
5006 Mc Lyon Paris 0.14
5007 Paul Adam Rome 0.13
5003 Lauson Hen San Jose 0.12
SELECT*
SELECTc.salesman_id, s.name, s.city, s.commission
FROM salesman
FROM where salesman s, customer c
WHERE salesman_id NOT IN (
group by s.salesman_id= c.salesman_id
Having count c.salesman_id, s.name SELECTa.salesman_id
FROM customer a, customer b
(c.salesman_id) = 1;
WHERE a.salesman_id = b.salesman_id
AND a.cust_name <> b.cust_name);
ADITI GUPTA
Query 10 (using subquery)
Display all the orders that had amounts that were greater than
at
least one of the orders from September 10th 2012.
SELECT*
FROM Orders
WHERE purch_amt> ANY
(SELECTpurch_amt
FROM orders
WHERE ord_date= '2012-09-10');
Query 11 (using subquery)
display only those customers whose grade are, in
fact, higher than every customer in New York.
customer_id cust_name city grade salesman_id
3008 Julian Green London 300 5002
3004 Fabian Johnson Paris 300 5006
SELECT*
FROM customer
WHERE grade > ALL
(SELECTgrade
FROM customer
WHERE city = 'NewYork');
"Data Analytics Using Power BI Live Workshop "
ADITI GUPTA