wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

SQL- Join, Group by, having

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

Which of the following SQL statements will retrieve only the records where there is a match between the Customers and Orders tables?

a)

SELECT * FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

b)

SELECT * FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

c)

SELECT * FROM Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

2.

What does the following query return?

sql?
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName, Customers.Country

FROM Orders

INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

a)

Orders that do not have any associated customers.

b)

All customers and the details of their orders

c)

All orders along with the customer details for the orders

d)

Customers that do not have any orders.

3.

Given the following SQL query, which table's data will be included in the result set even if there is no match in the other table?SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

a)

Customers

b)

Orders

c)

Both tables

d)

Neither Table

4.

How would you modify the following query to display the total number of orders placed by each customer?
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName, Customers.Country

FROM Orders

INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

a)

Add a GROUP BY clause to group by CustomerName

b)

Add a DISTINCT function and GROUP BY OrderID

c)

Add a COUNT function and GROUP BY OrderDate

d)

Add a COUNT function and GROUP BY CustomerName

5.

Which SQL clause is used to group rows that have the same values in specified columns?

a)

ORDER BY

b)

GROUP BY

c)

HAVING

d)

DISTINCT

6.

What will the following query return?
SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID GROUP BY ShipperName;

a)

The total number of orders delivered by each shipper

b)

The total number of orders placed by each customer

c)

The total number of orders with their shipper names

d)

he total number of customers who placed orders

7.

Which keyword is used to filter the grouped rows in the result set based on a condition?

a)

WHERE

b)

GROUP BY

c)

DISTINCT

d)

HAVING

8.

Given the following SQL query:

SELECT Customers.CustomerName, Orders.OrderID, Orders.OrderDate FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID WHERE Orders.OrderDate IS NULL;

What is the result of this query?

a)

It returns all customers who have placed at least one order

b)

It returns all customers and their order details, including those with no orders

c)

It returns customers who have not placed any orders

d)

It returns customers with orders that have a NULL OrderDate

9.

Given the following SQL query:
SELECT Orders.OrderID, Customers.CustomerName FROM Orders RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID WHERE Orders.OrderID IS NULL;

What does this query return?

a)

Orders that have no matching customer records

b)

Customers who have not placed any orders

c)

Orders with NULL CustomerID

d)

Customers with multiple orders

10.

Consider the following SQL query:

SELECT Customers.Country, COUNT(Orders.OrderID) AS TotalOrders FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID GROUP BY Customers.Country HAVING COUNT(Orders.OrderID) > 5;

What does this query return?

a)

All countries and their total number of orders

b)

Countries with more than 5 customers

c)

Countries with more than 5 orders

d)

Customers from countries that have placed exactly 5 orders