SQL Practice
SQL Practice
Follow Me
SQL Practice 1
One table, Aggregation, Group By
24 Queries
Link: https://www.w3resource.com/sql-exercises/
Manisha Reddy
Follow Me
Manisha Reddy
Follow Me
Query 1
• Display name and commission of all the salesmen.
Manisha Reddy
Follow Me
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
Query 2
• Retrieve salesman id of all salesmen from orders table without any
repeats.
Manisha Reddy
Follow Me
Query 2
• Retrieve salesman id of all
salesmen from orders table
without any repeats.
salesman_id
5002
5003
5006
5001
5005
5007
Query 3
• Display names and city of salesman, who belongs to the city of Paris.
Manisha Reddy
Follow Me
Query 3
• Display names and city of
salesman, who belongs to
the city of Paris.
name city
Nail Knite Paris
Mc Lyon Paris
SELECT name,city
FROM salesman
WHERE city='Paris';
Manisha Reddy
Follow Me
Query 4
• Display all the information for those customers with a
grade of 200.
SELECT *
FROM customer
WHERE grade = 200;
Manisha Reddy
Follow Me
Query 5
• Display the order number, order
date and the purchase amount for
order(s) which will be delivered by
the salesman with ID 5001.
winner
Pablo Neruda
SELECT winner
FROM nobel_win
WHERE year = 1971
AND subject = 'Literature';
Manisha Reddy
Follow Me
Query 7
• Show all the details of the winners with first name Louis.
SELECT *
FROM nobel_win
WHERE winner LIKE 'Louis%';
Manisha Reddy
Follow Me
Query 8
• Show all the winners in Physics for 1970 together with the winner of Economics for 1971.
SELECT *
FROM nobel_win
WHERE (subject = 'Physics' AND year = 1970)
UNION
(SELECT *
FROM nobel_win
WHERE (subject = 'Economics' AND year = 1971)
);
Manisha Reddy
Follow Me
Query 9
• Show all the winners of Nobel prize in the year 1970 except the subject Physiology and Economics.
SELECT *
FROM nobel_win
WHERE year = 1970
AND subject NOT IN ('Physiology','Economics');
Manisha Reddy
Follow Me
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
SELECT *
FROM customer
WHERE city = 'New York' OR NOT grade > 100;
Manisha Reddy
Follow Me
SELECT *
FROM customer
WHERE cust_name LIKE '%n';
Manisha Reddy
Follow Me
SELECT *
FROM salesman
WHERE name LIKE ‘N__l%';
Manisha Reddy
Follow Me
SELECT *
FROM customer
WHERE grade IS NULL;
Manisha Reddy
Follow Me
SELECT COUNT(*)
FROM orders
WHERE ord_date = '2012-08-17';
Manisha Reddy
Follow Me
SQL Practice 2
Multiple tables Joins Nested Queries
Link: https://www.w3resource.com/sql-exercises/
Manisha Reddy
Follow Me
Manisha Reddy
Follow Me
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
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 Nail Knite
Julian Green Nail Knite
Fabian Johnson Mc Lyon
Geoff Cameron Lauson Hen
Jozy Altidor Paul Adam
Brad Guzan Pit Alex
Query 3
• Display all those orders by the customers not located
in the same cities where their salesmen live.
SELECT *
FROM orders
WHERE salesman_id =
(SELECT salesman_id
FROM salesman
WHERE name = 'Paul Adam');
SELECT *
FROM orders
WHERE purch_amt >
(SELECT AVG(purch_amt)
FROM orders
WHERE ord_date = '2012-10-10');
SELECT *
FROM orders
WHERE salesman_id IN
(SELECT salesman_id
FROM salesman
WHERE city ='Paris');
SELECT *
FROM salesman
WHERE salesman_id IN (
SELECT DISTINCT salesman_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));
Manisha Reddy Follow Me
SELECT *
SELECT c.salesman_id, s.name, s.city, s.commission
FROM salesman
FROM salesman s, customer c
WHERE salesman_id NOT IN (
where s.salesman_id = c.salesman_id
SELECT a.salesman_id
group by c.salesman_id, s.name
FROM customer a, customer b
Having count(c.salesman_id) = 1;
WHERE a.salesman_id = b.salesman_id
AND a.cust_name <> b.cust_name);
Manisha Reddy Follow Me
SELECT *
FROM Orders
WHERE purch_amt > ANY
(SELECT purch_amt
FROM orders
WHERE ord_date = '2012-09-10');
Manisha Reddy Follow Me
SELECT *
FROM customer
WHERE grade > ALL
(SELECT grade
FROM customer
WHERE city = 'NewYork');