SQL Queries: Questions and Answers
1. List all the columns of the Salespeople table.
SELECT * FROM Salespeople;
2. List all customers with a rating of 100.
SELECT * FROM Customers WHERE rating = 100;
3. Find all records in the Customer table with NULL values in the city column.
SELECT * FROM Customers WHERE city IS NULL;
4. Find the largest order taken by each salesperson on each date.
SELECT snum, odate, MAX(amt) as largest_order
FROM Orders
GROUP BY snum, odate;
5. Arrange the Orders table by descending customer number.
SELECT * FROM Orders ORDER BY cnum DESC;
6. Find which salespeople currently have orders in the Orders table.
SELECT DISTINCT sname
FROM Salespeople s, Orders o
WHERE s.snum = o.snum;
7. List names of all customers matched with the salespeople serving them.
SELECT c.cname, s.sname
FROM Customers c, Salespeople s
WHERE c.snum = s.snum;
8. Find the names and numbers of all salespeople who had more than one customer.
SELECT s.snum, s.sname
FROM Salespeople s, Customers c
WHERE s.snum = c.snum
GROUP BY s.snum, s.sname
HAVING COUNT(c.snum) > 1;
9. Count the orders of each of the salespeople and output the results in descending order.
SELECT s.snum, s.sname, COUNT(o.onum) AS order_count
FROM Salespeople s, Orders o
WHERE s.snum = o.snum
GROUP BY s.sname, s.snum
ORDER BY order_count DESC;
10. List the Customer table if and only if one or more of the customers in the Customer table are
located in San Jose.
SELECT * FROM Customers WHERE city = 'San Jose';
11. Match salespeople to customers according to what city they lived in.
12. Find the largest order taken by each salesperson.