W3. SQL Group By Flashcards

1
Q

Q: What does the SQL GROUP BY statement do?

A

A: It groups rows that have the same values into summary rows, often used with aggregate functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: When is the GROUP BY statement commonly used?

A

A: It’s used with aggregate functions like COUNT(), MAX(), MIN(), SUM(), and AVG() to group results by one or more columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: Write the syntax for a GROUP BY statement.

A

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: Write a query to count the number of customers in each country.

A

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: How do you sort a GROUP BY result in descending order by count?

A

A: Use ORDER BY COUNT(column_name) DESC.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: Write a query to list the number of customers in each country, sorted high to low.

A

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: Can you use GROUP BY with JOIN statements?

A

A: Yes, GROUP BY can be used with JOIN to group results based on related data from multiple tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: Write a query to list the number of orders sent by each shipper.

A

SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM Orders
LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY ShipperName;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly