SQL Exercises
SQL Exercises
From the following tables write a SQL query to find the salesperson and customer who reside in the same city.
Return Salesman, cust_name and city.
2. From the following tables write a SQL query to find those orders where the order amount exists between 500
and 2000. Return ord_no, purch_amt, cust_name, city.
SELECT a.ord_no,a.ord_date,a.purch_amt,
b.cust_name AS "Customer Name", b.grade,
c.name AS "Salesman", c.commission
FROM orders a
INNER JOIN customer b
ON a.customer_id=b.customer_id
INNER JOIN salesman c
ON a.salesman_id=c.salesman_id;
7. Write a SQL statement to join the tables salesman, customer and orders so that the same column of each
table appears once and only the relational rows are returned.
SELECT *
FROM orders
NATURAL JOIN customer
NATURAL JOIN salesman;
8. From the following tables write a SQL query to display the customer name, customer city, grade, salesman,
salesman city. The results should be sorted by ascending customer_id.
SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman",b.city
FROM customer a
LEFT JOIN salesman b
ON a.salesman_id=b.salesman_id
order by a.customer_id;
9. From the following tables write a SQL query to find those customers with a grade less than 300. Return
cust_name, customer city, grade, Salesman, salesmancity. The result should be ordered by ascending
customer_id.
SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman", b.city
FROM customer a
LEFT OUTER JOIN salesman b
ON a.salesman_id=b.salesman_id
WHERE a.grade<300
ORDER BY a.customer_id;
10. Write a SQL statement to make a report with customer name, city, order number, order date, and order
amount in ascending order according to the order date to determine whether any of the existing customers
have placed an order or not.
12. Write a SQL statement to generate a list in ascending order of salespersons who work either for one or more
customers or have not yet joined any of the customers
SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman", b.city
FROM customer a
RIGHT OUTER JOIN salesman b
ON b.salesman_id=a.salesman_id
ORDER BY b.salesman_id;
13. From the following tables write a SQL query to list all salespersons along with customer name, city, grade,
order number, date, and amount.
SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman",
c.ord_no, c.ord_date, c.purch_amt
FROM customer a
RIGHT OUTER JOIN salesman b
ON b.salesman_id=a.salesman_id
RIGHT OUTER JOIN orders c
ON c.customer_id=a.customer_id;
14. Write a SQL statement to make a list for the salesmen who either work for one or more customers or yet to
join any of the customer. The customer may have placed, either one or more orders on or above order
amount 2000 and must have a grade, or he may not have placed any order to the associated supplier.
SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman",
c.ord_no, c.ord_date, c.purch_amt
FROM customer a
RIGHT OUTER JOIN salesman b
ON b.salesman_id=a.salesman_id
LEFT OUTER JOIN orders c
ON c.customer_id=a.customer_id
WHERE c.purch_amt>=2000
AND a.grade IS NOT NULL;
15. Write a SQL statement to generate a list of all the salesmen who either work for one or more customers or
have yet to join any of them. The customer may have placed one or more orders at or above order amount
2000, and must have a grade, or he may not have placed any orders to the associated supplier.
16. Write a SQL statement to generate a report with the customer name, city, order no. order date, purchase
amount for only those customers on the list who must have a grade and placed one or more orders or which
order(s) have been placed by the customer who neither is on the list nor has a grade.
SELECT *
FROM salesman a
CROSS JOIN customer b;
18. Write a SQL statement to create a Cartesian product between salesperson and customer, i.e. each salesperson
will appear for all customers and vice versa for that salesperson who belongs to that city.
SELECT *
FROM salesman a
CROSS JOIN customer b
WHERE a.city IS NOT NULL;
19. Write a SQL statement to create a Cartesian product between salesperson and customer, i.e. each salesperson
will appear for every customer and vice versa for those salesmen who belong to a city and customers who
require a grade
SELECT *
FROM salesman a
CROSS JOIN customer b
WHERE a.city IS NOT NULL
AND b.grade IS NOT NULL;
20. Write a SQL statement to make a Cartesian product between salesman and customer i.e. each salesman will
appear for all customers and vice versa for those salesmen who must belong to a city which is not the same as
his customer and the customers should have their own grade.
SELECT *
FROM salesman a
CROSS JOIN customer b
WHERE a.city IS NOT NULL
AND b.grade IS NOT NULL
AND a.city<>b.city;
21. From the following tables write a SQL query to select all rows from both participating tables as long as there is
a match between pro_com and com_id.
SELECT *
FROM item_mast
INNER JOIN company_mast
ON item_mast.pro_com= company_mast.com_id;
22. Write a SQL query to display the item name, price, and company name of all the products.
26. From the following tables write a SQL query to display all the data of employees including their department.
28. From the following tables write a SQL query to find the departments with budgets more than Rs. 50000 and
display the first name and last name of employees
SELECT emp_department.dpt_name
FROM emp_details
INNER JOIN emp_department
ON emp_dept =dpt_code
GROUP BY emp_department.dpt_name
HAVING COUNT(*) > 2;
SQL SUBQUERIES
1. From the following tables, write a SQL query to find all the orders issued by the salesman 'Paul Adam'.
Return ord_no, purch_amt, ord_date, customer_id and salesman_id
SELECT *
FROM orders
WHERE salesman_id =
(SELECT salesman_id
FROM salesman
WHERE name='Paul Adam');
2. From the following tables write a SQL query to find all orders generated by London-based salespeople.
Return ord_no, purch_amt, ord_date, customer_id, salesman_id
SELECT *
FROM orders
WHERE salesman_id IN
(SELECT salesman_id
FROM salesman
WHERE city='London');
3. From the following tables write a SQL query to find all orders generated by the salespeople who may work
for customers whose id is 3007. Return ord_no, purch_amt, ord_date, customer_id, salesman_id
SELECT *
FROM orders
WHERE salesman_id =
(SELECT DISTINCT salesman_id
FROM orders
WHERE customer_id =3007);
4. From the following tables write a SQL query to find the order values greater than the average order value
of 10th October 2012. Return ord_no, purch_amt, ord_date, customer_id, salesman_id
SELECT *
FROM orders
WHERE purch_amt >
(SELECT AVG(purch_amt)
FROM orders
WHERE ord_date ='10/10/2012');
5. From the following tables, write a SQL query to find all the orders generated in New York city. Return
ord_no, purch_amt, ord_date, customer_id and salesman_id
SELECT *
FROM orders
WHERE salesman_id IN
(SELECT salesman_id
FROM salesman
WHERE city ='New York');
6. From the following tables write a SQL query to determine the commission of the salespeople in Paris.
Return commission
SELECT commission
FROM salesman
WHERE salesman_id IN
(SELECT salesman_id
FROM customer
WHERE city = 'Paris');
7. Write a query to display all the customers whose ID is 2001 below the salesperson ID of Mc Lyon
SELECT *
FROM customer
WHERE customer_id =
(SELECT salesman_id -2001
FROM salesman
WHERE name = 'Mc Lyon');
8. From the following tables write a SQL query to count the number of customers with grades above the
average in New York City. Return grade and count
SELECT salesman_id,name
FROM salesman a
WHERE 1 <
(SELECT COUNT(*)
FROM customer
WHERE salesman_id=a.salesman_id);
12. From the following tables write a SQL query to find those orders, which are higher than the average
amount of the orders. Return ord_no, purch_amt, ord_date, customer_id and salesman_id
SELECT *
FROM orders a
WHERE purch_amt >
(SELECT AVG(purch_amt) FROM orders b
WHERE b.customer_id = a.customer_id);
13. From the following tables write a SQL query to find those orders that are equal or higher than the average
amount of the orders. Return ord_no, purch_amt, ord_date, customer_id and salesman_id
SELECT *
FROM orders a
WHERE purch_amt >=
(SELECT AVG(purch_amt) FROM orders b
WHERE b.customer_id = a.customer_id);
14. Write a query to find the sums of the amounts from the orders table, grouped by date, and eliminate all
dates where the sum was not at least 1000.00 above the maximum order amount for that date
Q1. Write an SQL query to fetch the current date-time from the system.
TO fetch the CURRENT DATE IN SQL Server
SELECT GETDATE();
Q2. Write a SQL query to fetch the PatientName in uppercase and state as lowercase. Also
use the ALIAS name for the result-set as PatName and NewState.
SELECT upper(patientname) as PatName, Lower(State) as Newstate from Table
Q3. Find the Nth highest consultation fees from the PatientsCheckup table with and without
using the TOP/LIMIT keywords.
Nth highest consultation fees from the PatientsCheckup table with using the TOP keywords
FROM(
FROM PatientsCheckup
Nth highest consultation fees from the PatientsCheckup table without using the TOP/LIMIT keywords.
SELECT ConsultationFees
FROM PatientsCheckup F1
WHERE N-1 = (
FROM PatientsCheckup F2
Q4. Write a query to fetch top N records using the TOP/LIMIT, ordered by
ConsultationFees.
Q5. Write a SQL query to create a table where the structure is copied from other table.
Q6. Write a query to fetch even and odd rows from a table.
SELECT * FROM (
FROM Patients
) P
Q7. Write an SQL query to fetch duplicate records from Patients, without considering the
primary key.
SELECT PatientName, DoctorID, RegDate, State, COUNT(*)
FROM Patients
Q8. Write a query to fetch the number of patients whose weight is greater than 68.
SELECT COUNT(*) FROM PatientsCheckup WHERE Weight > '68';
Q9. Write a query to retrieve the list of patients from the same state.
SELECT DISTINCT P.PatientID, P.PatientName, P.State
Q10. Write a query to retrieve two minimum and maximum consultation fees from the
PatientsCheckup Table.
[code]
[/code]
Copy code
Q11. Write a query to fetch patient details along with the weight fees, even if the details are
missing.
SELECT P.PatientName, C.ConsultationFees
FROM Patients P
LEFT JOIN
PatientsCheckup C
ON P.PatientId = C.PatientId;
Q12. Write a SQL query to fetch doctor wise count of patients sorted by the doctors.
SELECT DoctorID, COUNT(PatientID) AS DocPat
ORDER BY DocPat;
Q13. Write a SQL query to fetch the first and last record of the Patients table.
–FETCH FIRST RECORD
FROM PatientsCheckup
GROUP BY ConsultationFees
Q15. Write a SQL query to retrieve patient details from the Patients table who have a weight
in the PatientsCheckup table.
SELECT * FROM Patients P
WHERE EXISTS
Q16. Write a SQL query to retrieve the last 2 records from the Patients table.
SELECT * FROM Patients WHERE
Q17. Write a SQL query to find all the patients who joined in the year 2022.
–USING BETWEEN
– USING YEAR
Q18. Write a SQL query to fetch 50% records from the PatientsCheckup table.
SELECT *
Q19. Write a query to find those patients who have paid consultation fees between 400 to
700.
SELECT * FROM Patients WHERE PatientID IN
(SELECT PatientID FROM PatientsCheckup WHERE ConsultationFees BETWEEN '400' AND '700');
Copy code
Q20. Write a query to update the patient names by removing the leading and trailing spaces.
UPDATE Patients
Begin with A
Ends with S and contains 3 alphabets
Staying in the state Telangana
Q23. Write a SQL query to fetch details of all patients excluding patients with name
“Sheela” and “Anay”.
SELECT * FROM Patients WHERE PatientName NOT IN ('Sheela','Anay');
Q24. Write a query to fetch the total count of occurrences of a particular character – ‘x’ in
the PatientName.
SELECT PatientName, PatientID
FROM Patients;
Q25. Write a query to retrieve the first three characters of PatientName from the Patients
table.
SELECT SUBSTRING(PatientName, 1, 3) FROM Patients;
Q26. Write a query to fetch only the Address (string before space).
USING SUBSTRING
Q27. Write a query to combine Address and state into a new column – NewAddress.
SELECT CONCAT(Address, ' ', State) AS 'NewAddress' FROM Patients;
Both tables
One of the table. Let us say, patients present in Patients and not in the PatientsCheckup table.
WHERE PatientId IN
Q29. Write a query to find the number of patients whose RegDate is between 01/04/2021 to
31/12/2022 and are grouped according to state.
SELECT COUNT(*), State FROM Patients WHERE RegDate BETWEEN '01/04/2021' AND
'31/12/2022' GROUP BY State;
Q30. Write a query to fetch all records from the Patients table; ordered by PatientName in
ascending order, State in descending order.
SELECT * FROM Patients ORDER BY PatientName ASC, State DESC;