Questions: SQL & Relational DBs
Back to Topics List

SQL & Relational DBs Questions

Page 1 of 3 (Displaying Questions 1 – 100 of 215 Total)

1. What is a Database?

Show Answer

A database is an organized collection of information, or data, that is stored electronically in a computer system. Think of it as a digital filing cabinet.

Added: Nov 30, 2025

2. What is SQL?

Show Answer

SQL stands for Structured Query Language. It is the language used to talk to a database, allowing you to create, read, update, and delete data.

Added: Nov 30, 2025

3. What is a Table in a database?

Show Answer

A table is where data is actually stored. It is organized into rows and columns, similar to a spreadsheet.

Added: Nov 30, 2025

4. What is a Row (or Record)?

Show Answer

A row is a single entry or complete record in a table. For example, a row in an Employees table would contain all the information for one employee.

Added: Nov 30, 2025

5. What is a Column (or Field)?

Show Answer

A column is a set of data values of a particular type. For example, in an Employees table, "Name" would be a column.

Added: Nov 30, 2025

6. What is a Primary Key?

Show Answer

A Primary Key is a special column (or group of columns) that uniquely identifies every single row in a table. It ensures no two rows are identical.

Added: Nov 30, 2025

7. What is a Foreign Key?

Show Answer

A Foreign Key is a column in one table that links to the Primary Key in another table. It is used to create relationships between tables.

Added: Nov 30, 2025

8. What are the four main types of SQL commands (CRUD)?

Show Answer

The four main commands are: SELECT (to Read data), INSERT (to Create data), UPDATE (to Update data), and DELETE (to Delete data).

Added: Nov 30, 2025

9. What does the `SELECT` statement do?

Show Answer

The `SELECT` statement is used to retrieve data from a database table. It is the most common command.

Added: Nov 30, 2025

10. What does the `FROM` clause specify?

Show Answer

The `FROM` clause specifies the table or tables you want to retrieve the data from.

Added: Nov 30, 2025

11. Write a query to fetch all columns and all rows from a table called `Customers`.

Show Answer

SELECT * FROM Customers;

Added: Nov 30, 2025

12. Write a query to fetch only the `Name` and `Email` columns from the `Employees` table.

Show Answer

SELECT Name, Email FROM Employees;

Added: Nov 30, 2025

13. Write a query to fetch the name of every product from the `Products` table.

Show Answer

SELECT ProductName FROM Products;

Added: Nov 30, 2025

14. How do you select unique (non-duplicate) values from the `City` column in the `Customers` table?

Show Answer

SELECT DISTINCT City FROM Customers;

Added: Nov 30, 2025

15. Write a query to change the column name `Name` to `FullName` in the results you see (aliasing).

Show Answer

SELECT Name AS FullName FROM Employees;

Added: Nov 30, 2025

16. Write a query that combines the `FirstName` and `LastName` columns into one column called `FullName` from the `Users` table.

Show Answer

SELECT FirstName || ' ' || LastName AS FullName FROM Users;

Added: Nov 30, 2025

17. Write a query to show today's date.

Show Answer

SELECT CURDATE();

Added: Nov 30, 2025

18. Write a query to select all orders that have a status of 'Pending'.

Show Answer

SELECT * FROM Orders WHERE Status = 'Pending';

Added: Nov 30, 2025

19. Write a query to find all employees who are NOT in the 'IT' department.

Show Answer

SELECT * FROM Employees WHERE Department <> 'IT';

Added: Nov 30, 2025

20. Write a query to find products whose price is exactly 50.

Show Answer

SELECT ProductName FROM Products WHERE Price = 50;

Added: Nov 30, 2025

21. Write a query to find products with a price greater than 100.

Show Answer

SELECT ProductName FROM Products WHERE Price > 100;

Added: Nov 30, 2025

22. Write a query to find customers with an ID less than 10.

Show Answer

SELECT * FROM Customers WHERE CustomerID < 10;

Added: Nov 30, 2025

23. Write a query to find all orders placed in the year 2023.

Show Answer

SELECT * FROM Orders WHERE YEAR(OrderDate) = 2023;

Added: Nov 30, 2025

24. Write a query to find employees whose salary is between 40000 and 60000 (inclusive).

Show Answer

SELECT Name, Salary FROM Employees WHERE Salary BETWEEN 40000 AND 60000;

Added: Nov 30, 2025

25. Write a query to find products whose `Category` is either 'Electronics' or 'Books'.

Show Answer

SELECT * FROM Products WHERE Category IN ('Electronics', 'Books');

Added: Nov 30, 2025

26. Write a query to find customers who live in 'London' and have an age over 30.

Show Answer

SELECT * FROM Customers WHERE City = 'London' AND Age > 30;

Added: Nov 30, 2025

27. Write a query to find employees in 'Sales' OR those who have a salary over 70000.

Show Answer

SELECT * FROM Employees WHERE Department = 'Sales' OR Salary > 70000;

Added: Nov 30, 2025

28. Write a query to find customers whose email address is missing (NULL).

Show Answer

SELECT * FROM Customers WHERE Email IS NULL;

Added: Nov 30, 2025

29. Write a query to find all products whose name starts with the letter 'A'.

Show Answer

SELECT ProductName FROM Products WHERE ProductName LIKE 'A%';

Added: Nov 30, 2025

30. Write a query to find customers whose name has 'son' in it, anywhere (e.g., Johnson, Anderson).

Show Answer

SELECT Name FROM Customers WHERE Name LIKE '%son%';

Added: Nov 30, 2025

31. Write a query to find all products whose name contains the letter 'z'.

Show Answer

SELECT ProductName FROM Products WHERE ProductName LIKE '%z%';

Added: Nov 30, 2025

32. Write a query to find orders placed in the month of May, regardless of the year.

Show Answer

SELECT * FROM Orders WHERE MONTH(OrderDate) = 5;

Added: Nov 30, 2025

33. Write a query to find all employees who are NOT managers.

Show Answer

SELECT * FROM Employees WHERE JobTitle <> 'Manager';

Added: Nov 30, 2025

34. Write a query to find products that are NOT in the 'Dairy' and NOT in the 'Meat' category.

Show Answer

SELECT * FROM Products WHERE Category NOT IN ('Dairy', 'Meat');

Added: Nov 30, 2025

35. Write a query to find customers who live in a city starting with 'N'.

Show Answer

SELECT Name, City FROM Customers WHERE City LIKE 'N%';

Added: Nov 30, 2025

36. Write a query to find employees whose name is exactly 4 characters long.

Show Answer

SELECT Name FROM Employees WHERE Name LIKE '____';

Added: Nov 30, 2025

37. Write a query to find products with a price greater than or equal to 9.99.

Show Answer

SELECT ProductName FROM Products WHERE Price >= 9.99;

Added: Nov 30, 2025

38. Write a query to find all orders that were placed before the current date.

Show Answer

SELECT * FROM Orders WHERE OrderDate < CURDATE();

Added: Nov 30, 2025

39. Write a query to find employees whose hire date is between 2020 and 2022 (inclusive of years).

Show Answer

SELECT Name, HireDate FROM Employees WHERE YEAR(HireDate) BETWEEN 2020 AND 2022;

Added: Nov 30, 2025

40. Write a query to find customers who have an email but their address is NULL.

Show Answer

SELECT * FROM Customers WHERE Email IS NOT NULL AND Address IS NULL;

Added: Nov 30, 2025

41. Write a query to show all employees, ordered from lowest salary to highest salary.

Show Answer

SELECT * FROM Employees ORDER BY Salary ASC;

Added: Nov 30, 2025

42. Write a query to show the top 5 most expensive products.

Show Answer

SELECT ProductName, Price FROM Products ORDER BY Price DESC LIMIT 5;

Added: Nov 30, 2025

43. Write a query to sort customers first by their `City` (A-Z), and then by their `Age` (highest to lowest).

Show Answer

SELECT * FROM Customers ORDER BY City ASC, Age DESC;

Added: Nov 30, 2025

44. Write a query to find the single cheapest product.

Show Answer

SELECT ProductName, Price FROM Products ORDER BY Price ASC LIMIT 1;

Added: Nov 30, 2025

45. Write a query to get the 3rd to 5th most recently placed orders.

Show Answer

SELECT * FROM Orders ORDER BY OrderDate DESC LIMIT 3 OFFSET 2;

Added: Nov 30, 2025

46. Write a query to list all employees ordered alphabetically by their Name.

Show Answer

SELECT Name FROM Employees ORDER BY Name ASC;

Added: Nov 30, 2025

47. Write a query to get the bottom 10 products by price.

Show Answer

SELECT ProductName, Price FROM Products ORDER BY Price ASC LIMIT 10;

Added: Nov 30, 2025

48. Write a query to sort employees by their hire date, starting with the oldest hire.

Show Answer

SELECT Name, HireDate FROM Employees ORDER BY HireDate ASC;

Added: Nov 30, 2025

49. Write a query to show the 2 most recent orders.

Show Answer

SELECT * FROM Orders ORDER BY OrderDate DESC LIMIT 2;

Added: Nov 30, 2025

50. Write a query to list products by price, without specifying ASC (ascending) or DESC (descending).

Show Answer

SELECT ProductName, Price FROM Products ORDER BY Price;

Added: Nov 30, 2025

51. Write a query to count the total number of employees in the `Employees` table.

Show Answer

SELECT COUNT(*) FROM Employees;

Added: Nov 30, 2025

52. Write a query to find the highest salary in the `Employees` table.

Show Answer

SELECT MAX(Salary) FROM Employees;

Added: Nov 30, 2025

53. Write a query to find the lowest price of any product.

Show Answer

SELECT MIN(Price) FROM Products;

Added: Nov 30, 2025

54. Write a query to calculate the average price of all products.

Show Answer

SELECT AVG(Price) FROM Products;

Added: Nov 30, 2025

55. Write a query to calculate the total amount of all orders combined.

Show Answer

SELECT SUM(TotalAmount) FROM Orders;

Added: Nov 30, 2025

56. Write a query to count how many customers are in each `City`.

Show Answer

SELECT City, COUNT(*) AS CustomerCount FROM Customers GROUP BY City;

Added: Nov 30, 2025

57. Write a query to find the total salary sum for each `Department`.

Show Answer

SELECT Department, SUM(Salary) AS TotalDeptSalary FROM Employees GROUP BY Department;

Added: Nov 30, 2025

58. Write a query to find the average order amount for each `Customer`.

Show Answer

SELECT CustomerID, AVG(TotalAmount) FROM Orders GROUP BY CustomerID;

Added: Nov 30, 2025

59. Write a query to count the number of products in each `Category`, but only show categories with more than 10 products.

Show Answer

SELECT Category, COUNT(*) FROM Products GROUP BY Category HAVING COUNT(*) > 10;

Added: Nov 30, 2025

60. Write a query to find departments where the average salary is greater than 50000.

Show Answer

SELECT Department FROM Employees GROUP BY Department HAVING AVG(Salary) > 50000;

Added: Nov 30, 2025

61. Write a query to find the maximum price of products, grouped by their `Category`.

Show Answer

SELECT Category, MAX(Price) FROM Products GROUP BY Category;

Added: Nov 30, 2025

62. Write a query to count the total number of distinct job titles in the `Employees` table.

Show Answer

SELECT COUNT(DISTINCT JobTitle) FROM Employees;

Added: Nov 30, 2025

63. Write a query to find the minimum order date and maximum order date in the `Orders` table.

Show Answer

SELECT MIN(OrderDate) AS FirstOrder, MAX(OrderDate) AS LastOrder FROM Orders;

Added: Nov 30, 2025

64. Write a query to find the department with the fewest employees.

Show Answer

SELECT Department FROM Employees GROUP BY Department ORDER BY COUNT(*) ASC LIMIT 1;

Added: Nov 30, 2025

65. Write a query to find the average salary in the 'Marketing' department.

Show Answer

SELECT AVG(Salary) FROM Employees WHERE Department = 'Marketing';

Added: Nov 30, 2025

66. Write a query to find the total quantity of a product with `ProductID` 10 sold across all orders.

Show Answer

SELECT SUM(Quantity) FROM OrderItems WHERE ProductID = 10;

Added: Nov 30, 2025

67. Write a query to count the number of orders placed on a specific date, '2024-01-15'.

Show Answer

SELECT COUNT(*) FROM Orders WHERE OrderDate = '2024-01-15';

Added: Nov 30, 2025

68. Write a query to find the maximum price for a product in the 'Beverages' category.

Show Answer

SELECT MAX(Price) FROM Products WHERE Category = 'Beverages';

Added: Nov 30, 2025

69. Write a query to find the number of distinct products sold.

Show Answer

SELECT COUNT(DISTINCT ProductID) FROM OrderItems;

Added: Nov 30, 2025

70. Write a query to find customers whose average order amount is over 1000.

Show Answer

SELECT CustomerID FROM Orders GROUP BY CustomerID HAVING AVG(TotalAmount) > 1000;

Added: Nov 30, 2025

71. Write a query to insert a new customer named 'Alice' with email 'alice@example.com' into the `Customers` table.

Show Answer

INSERT INTO Customers (Name, Email) VALUES ('Alice', 'alice@example.com');

Added: Nov 30, 2025

72. Write a query to insert a new product: 'Laptop' with a price of 1200 and category 'Electronics'.

Show Answer

INSERT INTO Products (ProductName, Price, Category) VALUES ('Laptop', 1200.00, 'Electronics');

Added: Nov 30, 2025

73. Write a query to change the price of the product named 'Laptop' to 1300.

Show Answer

UPDATE Products SET Price = 1300.00 WHERE ProductName = 'Laptop';

Added: Nov 30, 2025

74. Write a query to change the `City` of the employee with `EmployeeID` 101 to 'New York'.

Show Answer

UPDATE Employees SET City = 'New York' WHERE EmployeeID = 101;

Added: Nov 30, 2025

75. Write a query to give every employee in the 'Sales' department a 10% raise.

Show Answer

UPDATE Employees SET Salary = Salary * 1.10 WHERE Department = 'Sales';

Added: Nov 30, 2025

76. Write a query to delete the customer with `CustomerID` 5.

Show Answer

DELETE FROM Customers WHERE CustomerID = 5;

Added: Nov 30, 2025

77. Write a query to delete all orders that have a status of 'Cancelled'.

Show Answer

DELETE FROM Orders WHERE Status = 'Cancelled';

Added: Nov 30, 2025

78. Write a query to permanently remove the `Products` table and all its data.

Show Answer

DROP TABLE Products;

Added: Nov 30, 2025

79. Write a query to remove all rows from the `Logins` table, but keep the table structure.

Show Answer

TRUNCATE TABLE Logins;

Added: Nov 30, 2025

80. Write a query to add a new column called `PhoneNumber` (text, max 15 characters) to the `Customers` table.

Show Answer

ALTER TABLE Customers ADD PhoneNumber VARCHAR(15);

Added: Nov 30, 2025

81. Write a query to get the full details of the employee who earns the minimum salary.

Show Answer

SELECT * FROM Employees WHERE Salary = (SELECT MIN(Salary) FROM Employees);

Added: Nov 30, 2025

82. Write a query to find all products that are more expensive than the average price of all products.

Show Answer

SELECT ProductName, Price FROM Products WHERE Price > (SELECT AVG(Price) FROM Products);

Added: Nov 30, 2025

83. What SQL command is used to add new tables or columns to a database?

Show Answer

The `CREATE` and `ALTER` commands are used. `CREATE TABLE` makes a new table, and `ALTER TABLE` changes an existing one.

Added: Nov 30, 2025

84. What is an Index in a database?

Show Answer

An index is a special lookup table that the database search engine can use to speed up data retrieval. It is like an index in the back of a book.

Added: Nov 30, 2025

85. What is the `JOIN` command used for?

Show Answer

The `JOIN` command is used to combine rows from two or more tables based on a related column between them (usually a Foreign Key).

Added: Nov 30, 2025

86. Write a query to combine a customer's `Name` from the `Customers` table with their `OrderDate` from the `Orders` table (joining on CustomerID).

Show Answer

SELECT c.Name, o.OrderDate FROM Customers c JOIN Orders o ON c.CustomerID = o.CustomerID;

Added: Nov 30, 2025

87. What is an INNER JOIN?

Show Answer

An Inner Join returns only the rows that have matching values in both tables being joined.

Added: Nov 30, 2025

88. What is a LEFT JOIN (or LEFT OUTER JOIN)?

Show Answer

A Left Join returns all rows from the first (left) table and the matching rows from the second (right) table. If there is no match, it returns NULL for the right table's columns.

Added: Nov 30, 2025

89. Write a query to show all customers and their order IDs, including customers who have not placed any orders.

Show Answer

SELECT c.Name, o.OrderID FROM Customers c LEFT JOIN Orders o ON c.CustomerID = o.CustomerID;

Added: Nov 30, 2025

90. Write a query to find all products that have no matching sales records in the `Sales` table (using LEFT JOIN).

Show Answer

SELECT p.ProductName FROM Products p LEFT JOIN Sales s ON p.ProductID = s.ProductID WHERE s.ProductID IS NULL;

Added: Nov 30, 2025

91. Write a query to change the name of the column `Email` to `ContactEmail` in the `Employees` table.

Show Answer

ALTER TABLE Employees RENAME COLUMN Email TO ContactEmail;

Added: Nov 30, 2025

92. Write a query to extract only the year from the `OrderDate` column.

Show Answer

SELECT YEAR(OrderDate) AS OrderYear FROM Orders;

Added: Nov 30, 2025

93. Write a query to combine the results of two SELECT statements (e.g., combining all names from `Employees` and `Customers`).

Show Answer

SELECT Name FROM Employees UNION SELECT Name FROM Customers;

Added: Nov 30, 2025

94. What is the difference between `UNION` and `UNION ALL`?

Show Answer

`UNION` combines the results of two queries and removes duplicate rows. `UNION ALL` combines results and keeps all rows, including duplicates, which is usually faster.

Added: Nov 30, 2025

95. How do you permanently save the changes you made with `INSERT`, `UPDATE`, or `DELETE` commands?

Show Answer

You use the `COMMIT` command. This makes the changes permanent in the database.

Added: Nov 30, 2025

96. What command do you use to undo changes made in a transaction before they are saved?

Show Answer

You use the `ROLLBACK` command to undo the changes since the last `COMMIT`.

Added: Nov 30, 2025

97. Write a query to create a simple table called `Tasks` with two columns: `TaskID` (number, primary key) and `Description` (text).

Show Answer

CREATE TABLE Tasks (TaskID INT PRIMARY KEY, Description VARCHAR(255));

Added: Nov 30, 2025

98. Write a query to create an index named `idx_product_name` on the `ProductName` column of the `Products` table.

Show Answer

CREATE INDEX idx_product_name ON Products (ProductName);

Added: Nov 30, 2025

99. Write a query to find all customers who have never placed an order (using `NOT IN`).

Show Answer

SELECT Name FROM Customers WHERE CustomerID NOT IN (SELECT DISTINCT CustomerID FROM Orders);

Added: Nov 30, 2025

100. Write a query to count the total number of tables in your current database.

Show Answer

SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE();

Added: Nov 30, 2025