Navigate to W3 School's Database to get started.
If you need a SQL refresher, visit the SQL Start Guide.
- Basic Queries
- Calculated Fields
- Grouping Data
- Join Queries
- Union
- Case Examples
- Concepts Short Answer Questions
-
Query all OrderDetails where order quantity is greater than 10 but less than 30
Reveal Solution
Solution #1
SELECT * FROM OrderDetails WHERE Quantity > 10 AND Quantity < 30;
Solution #2
SELECT * FROM OrderDetails WHERE Quantity BETWEEN 11 AND 29;
-
Query the names of all Suppliers that are located in either the USA, UK, or Canada
Reveal Solution
SELECT * FROM Suppliers WHERE Country IN ("USA", "UK", "Canada")
-
Query the FirstName of Employees who's FirstName starts with the letter A then sort by FirstName in descending order
Reveal Solution
Solution #1
SELECT * FROM Employees WHERE LEFT(FirstName, 1) = 'A' ORDER BY FirstName DESC
Solution #2
SELECT * FROM Employees WHERE SUBSTRING(FirstName, 1, 1) = 'A' ORDER BY FirstName DESC
-
Query Employees to return employee FirstName & LastName together under new calculated field, FullName
Reveal Solution
SELECT CONCAT(FirstName, LastName) AS FullName FROM Employees
-
Query Customers to return two new calculated fields
-
ContactName's first name under new calculated field, FirstName
-
ContactName's last name under new calculated field, LastName
Reveal Solution
SELECT LEFT(ContactName, CHARINDEX(' ', ContactName)) AS FirstName, RIGHT(ContactName, LEN(ContactName) - CHARINDEX(' ', ContactName)) As LastName FROM Customers
-
-
Query Products to return two new calculated fields:
-
The price divided by 2 under new calculated field, HalfOff (Round to nearest 2 decimals)
-
The price plus 8% tax under new calculated field, NetPrice (Round to nearest 2 decimals)
Reveal Solution
SELECT ROUND(Price/2, 2) AS HalfOff, ROUND(Price * 1.08, 2) As NetPrice FROM Products
-
-
Query OrderDetails to calculate the average quantity order per ProductID under new calculated field, AvgQuantity
Reveal Solution
SELECT AVG(Quantity) As AvgQuantity FROM OrderDetails GROUP BY ProductID
-
Query Countries from Customers and calculate the total Customers per Country under new calculated field, TotalCustomers. Finally, sort TotalCustomers from highest to lowest.
Reveal Solution
SELECT Country, SUM(CustomerID) As TotalCustomers FROM Customers GROUP BY Country ORDER BY TotalCustomers DESC
-
Query all OrderDetails along with ProductName & Price and return (Quantity * Price) under new calculated field, Total
-
Query all Orders details along with Customer Name & Employee FirstName & LastName as new calculated field, EmployeeName
-
Query all Shippers details and return the total number of Products shipped under new calculated field, TotalShippedProducts
-
Query the names and phone numbers across all Shippers and Suppliers
-
Query the fullnames of all customer and employee with the letter "a" is in their First or Last Name (be careful for case sensitivity)
-
Query all Products details and return new calculated field, PriceRange with the following conditions
- If the product price is below or equal to 15, return "Low"
- If the product price is above 15 and below/ equal to 30, return "Med"
- If the product price is above 30, return "High"
-
Query all OrderDetails and return new calculated field, EvenOdd with the following conditions
- If the order quantity is even (e.g. 2, 4, 6), return "Even"
- If the order quantity is odd (e.g. 1, 3, 5), return "Odd"
-
Query all Customers details and return new calculated field, Location with the following conditions
- If the customer's country is located in the USA, return "Domestic"
- If the customer's country is not located in the USA, return "International"
-
What is a join in SQL? What are the types of joins?
-
What is the difference between a UNION and a JOIN query?
-
What is a foreign key?
-
What is the difference between a database versus a table versus a field?