0% found this document useful (0 votes)
3 views

SQL_Practice1

The document outlines a series of SQL practice queries, covering various topics such as aggregation, joins, and nested queries. It includes examples of SQL commands to retrieve data from different tables, along with a link to a workshop on Data Analytics using Power BI. Additionally, it provides specific queries for displaying information about salesmen, customers, and orders, demonstrating practical SQL applications.

Uploaded by

sai Charan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL_Practice1

The document outlines a series of SQL practice queries, covering various topics such as aggregation, joins, and nested queries. It includes examples of SQL commands to retrieve data from different tables, along with a link to a workshop on Data Analytics using Power BI. Additionally, it provides specific queries for displaying information about salesmen, customers, and orders, demonstrating practical SQL applications.

Uploaded by

sai Charan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

SQL Practice

One table, Aggregation, Group


By 24 Queries

Link: https://www.w3resource.com/sql-exercises/
"Data Analytics Using Power BI Live Workshop "

If You are interested in Learning Data Analytics with Power BI,


You can enroll for the online live Workshop

Sunday 10:00 AM Onwards


Designed for beginners.
Complete practical and interactive live workshop.
Includes an end to end project and certificate.

Here is the link to register -


https://analytics.techtip24workshop.com/

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.

customer_id cust_name city grade salesman_id


3007 Brad Davis New York 200 5001
3005 Graham Zusi California 200 5002
3003 Jozy Altidor Moscow 200 5007

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.

ord_no ord_date purch_amt


70002 2012-10-05 65.26
70005 2012-07-27 2400.60
70008 2012-09-10 5760.00
70013 2012-04-25 3045.60

SELECTord_no, ord_date, purch_amt


FROM orders
WHERE salesman_id= 5001;
Query 6 (table: nobel_win)
• Show the winner of the 1971 prize for Literature.

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.

year subject winner country category


1970 Physics Louis Neel France Scientist

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.

year subject winner country category


1970 Physics Hannes Alfven Sweden Scientist
1970 Physics Louis Neel France Scientist
1971 Economic Simon Kuznets Russia Economist
s
SELECT*
FROM nobel_win
WHERE (subject = 'Physics' AND year = 1970)
UNION
(SELECT*
FROM nobel_win
WHERE (subject = 'Economics' AND year = 1971)
);
Query 9
• Show all the winners of Nobel prize in the year 1970 except the subject Physiology and Economics.

year subject winner country category


1970 Physics Hannes Alfven Sweden Scientist
1970 Physics Louis Neel France Scientist
1970 Chemistry Luis Federico Leloir France Scientist
1970 Literature Aleksandr Solzhenitsyn Russia Linguist

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.

year subject winner country category


1994 Literature Kenzaburo Oe Japan Linguist
1994 Economics Reinhard Selten Germany Economist SELECT*
1987 Chemistry Donald J. Cram USA Scientist FROM nobel_win
1987 Chemistry Jean-Marie Lehn France Scientist WHERE subject NOT LIKE 'P%'
1987 Literature Joseph Brodsky Russia Linguist ORDER BY year DESC, winner;
1987 Economics Robert Solow USA Economist
1971 Chemistry Gerhard Herzberg Germany Scientist
1971 Literature Pablo Neruda Chile Linguist
1971 Economics Simon Kuznets Russia Economist
1970 Literature Aleksandr Solzhenitsyn Russia Linguist
1970 Chemistry Luis Federico Leloir France Scientist
1970 Economics Paul Samuelson USA Economist
Query 11 (table: item_mast)
• Find the name and price of the cheapest item(s).

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.

customer_id cust_name city grade salesman_id


3002 Nick Rimando New York 100 5001
3007 Brad Davis New York 200 5001
3009 Geoff Cameron Berlin 100 5003

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.

SELECTsalesman_id, name, city, commission


FROM salesman
WHERE (commission > 0.10 ANDcommission < 0.12);

SELECTsalesman_id, name, city, commission


FROM salesman
WHERE commission between 0.10 AND0.12;
Query 14 (table: customer)
• Find all those customers with all information whose names are ending with the letter 'n'.

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;

SELECT COUNT (DISTINCTsalesman_id)


FROM orders;
Query 19 (table: customer)
• Find the highest grade for each of the cities of the customers.

SELECTcity, MAX (grade)


FROM customer
GROUP BY city;
Query 20 (table: orders)
• Find the highest purchase amount ordered by the each customer
with their ID and highest purchase amount.

SELECTcustomer_id, MAX (purch_amt)


FROM orders
GROUP BY customer_id;
Query 21 (table: orders)
• Find the highest purchase amount ordered by the each
customer on a particular date with their ID, order date
and highest purchase amount.

SELECTcustomer_id, ord_date, MAX (purch_amt)


FROM orders
GROUP BY customer_id, ord_date;
Query 22 (table: orders)
• Find the highest purchase amount on a date '2012-08-17'
for each salesman with their ID.

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.

SELECTcustomer_id, ord_date, MAX(purch_amt)


FROM orders
GROUP BY customer_id, ord_date
HAVING MAX (purch_amt) > 2000.00;
Query 24 (table: orders)
• Write a SQL statement that counts all orders for a
date August 17th, 2012.

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 ord_no cust_name orders.customer_id


orders.salesman_id
FROMsalesman customer orders
WHERE customer.city <> salesman.city
AND orders.customer_id = customer.customer_id
AND orders.salesman_id = salesman.salesman_id;
Query 4 (using subquery)
Display all the orders issued by the salesman 'Paul
Adam' from the orders table.

ord_no purch_amt ord_date customer_id salesman_id


70011 75.29 2012-08-17 3003 5007

SELECT*
FROM orders
WHERE salesman_id=
(SELECTsalesman_id
FROM salesman
WHERE name = 'Paul Adam');

•Can we make this query unnested? If yes how?


Query 5 (using subquery)
Display all the orders which values are greater than
the average order value for 10th October 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
70003 2480.40 2012-10-10 3009 5003
70013 3045.60 2012-04-25 3002 5001

SELECT *
FROM orders
WHERE purch_amt >
(SELECTAVG(purch_amt)
FROMorders
WHEREord_date= '2012-10-10');

•Can we make this query unnested? If yes how?


Query 6 (using subquery)
Findall orders attributed to salesmen in Paris.

ord_no purch_amt ord_date customer_id salesman_id


70001 150.50 2012-10-05 3005 5002
70007 948.50 2012-09-10 3005 5002
70012 250.45 2012-06-27 3008 5002
70010 1983.43 2012-10-10 3004 5006

SELECT*
FROM orders
WHERE salesman_idIN
(SELECTsalesman_id
FROM salesman
WHERE city ='Paris');

•Can we make this query unnested? If yes how?


Query 7 (using subquery)
Extract the data from the orders table for the
salesman who earned the maximum commission.

ord_no purch_amt ord_date salesman_id


70002 65.26 2012-10-05 5001
70005 2400.60 2012-07-27 5001
70008 5760.00 2012-09-10 5001
70013 3045.60 2012-04-25 5001

SELECTord_no, purch_amt, ord_date, salesman_id


FROM orders
WHERE salesman_idIN (
SELECT salesman_id
FROMsalesman
WHEREcommission =(
SELECTMAX(commission)
FROM salesman)
);
Query 8 (using subquery)
Find the name and ids of all salesmen who
had more than one customer.
salesman_id name
5001 James Hoog
5002 Nail Knite

SELECTsalesman_id, name
FROM salesman ASa
WHERE 1 <
(SELECTCOUNT(*)
FROM customer ASc
WHERE c.salesman_id= a.salesman_id);

•Can we make this query unnested? If yes how?

SELECTc.salesman_id, s.name FROMsalesman AS s, customer AS c


where s.salesman_id= c.salesman_id
group by c.salesman_id, s.name Havingcount(c.salesman_id) > 1;
Query 9 (using subquery)
Write a query to find all the salesmen who
worked for only one customer.

salesman_id name city commission


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*
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.

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
(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 "

If You are interested in Learning Data Analytics with Power BI,


You can enroll for the online live Workshop

Sunday 10:00 AM Onwards


Designed for beginners.
Complete practical and interactive live workshop.
Includes an end to end project and certificate.

Here is the link to register -


https://analytics.techtip24workshop.com/

ADITI GUPTA

You might also like