SQL Practice2
SQL Practice2
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
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 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));
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 *
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);
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.
ord_no purch_amt ord_date customer_id salesman_id
70005 2400.60 2012-07-27 3007 5001
70008 5760.00 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.40 2012-10-10 3009 5003
70013 3045.60 2012-04-25 3002 5001
70007 948.50 2012-09-10 3005 5002
SELECT *
FROM Orders
WHERE purch_amt > ANY
(SELECT purch_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
(SELECT grade
FROM customer
WHERE city = 'NewYork');