Questions: SQL & Relational DBs
Back to Topics List

SQL & Relational DBs Questions

Page 2 of 3 (Displaying Questions 101 – 200 of 215 Total)

101. What is a Data Type in SQL?

Show Answer

A data type defines what kind of data a column can hold, such as numbers (INT), text (VARCHAR), or dates (DATE).

Added: Nov 30, 2025

102. How do you check the structure or definition of a table called `Projects`?

Show Answer

DESCRIBE Projects; (or SHOW COLUMNS FROM Projects;)

Added: Nov 30, 2025

103. Write a query to calculate a 15% bonus on the `Salary` column for all employees.

Show Answer

SELECT Name, Salary * 0.15 AS Bonus FROM Employees;

Added: Nov 30, 2025

104. Write a query to find all customers whose name does NOT start with the letter 'S'.

Show Answer

SELECT Name FROM Customers WHERE Name NOT LIKE 'S%';

Added: Nov 30, 2025

105. Write a query to find all orders placed on a Monday.

Show Answer

SELECT * FROM Orders WHERE DAYOFWEEK(OrderDate) = 2;

Added: Nov 30, 2025

106. What is the purpose of the `GROUP BY` clause?

Show Answer

The `GROUP BY` clause is used to arrange identical data into groups. It is often used with aggregate functions (like COUNT, SUM) to perform calculations on each group.

Added: Nov 30, 2025

107. What is the difference between `COUNT(*)` and `COUNT(ColumnName)`?

Show Answer

`COUNT(*)` counts all rows in a table, including rows with NULL values. `COUNT(ColumnName)` counts only the rows where that specific column is NOT NULL.

Added: Nov 30, 2025

108. Write a query to find all products that are NOT currently on sale (assuming an `OnSale` column where 1 means Yes, 0 means No).

Show Answer

SELECT ProductName FROM Products WHERE OnSale = 0;

Added: Nov 30, 2025

109. Write a query to find employees in the 'HR' department whose salary is less than 50000.

Show Answer

SELECT * FROM Employees WHERE Department = 'HR' AND Salary < 50000;

Added: Nov 30, 2025

110. Write a query to delete all products with a price of zero.

Show Answer

DELETE FROM Products WHERE Price = 0;

Added: Nov 30, 2025

111. Write a query to rename the table `OldTableName` to `NewTableName`.

Show Answer

RENAME TABLE OldTableName TO NewTableName;

Added: Nov 30, 2025

112. What is a View in SQL?

Show Answer

A View is a virtual table based on the result-set of an SQL query. It does not store data itself, but shows data from one or more tables as if it were a single table.

Added: Nov 30, 2025

113. Write a query to find customers whose `Age` is NOT NULL (they have an age recorded).

Show Answer

SELECT Name FROM Customers WHERE Age IS NOT NULL;

Added: Nov 30, 2025

114. What does the `HAVING` clause do?

Show Answer

The `HAVING` clause is used to filter groups based on a condition, after they have been grouped by the `GROUP BY` clause. It is like a WHERE clause for groups.

Added: Nov 30, 2025

115. Write a query to update the `Status` of an order with `OrderID` 205 to 'Shipped'.

Show Answer

UPDATE Orders SET Status = 'Shipped' WHERE OrderID = 205;

Added: Nov 30, 2025

116. Write a query to find employees whose names start with either 'J' or 'M'.

Show Answer

SELECT Name FROM Employees WHERE Name LIKE 'J%' OR Name LIKE 'M%';

Added: Nov 30, 2025

117. Write a query to find products that cost less than 20 OR more than 500.

Show Answer

SELECT ProductName FROM Products WHERE Price < 20 OR Price > 500;

Added: Nov 30, 2025

118. Write a query to find customers who are NOT from 'France' and NOT from 'Germany'.

Show Answer

SELECT * FROM Customers WHERE Country NOT IN ('France', 'Germany');

Added: Nov 30, 2025

119. How do you ensure a column, like `Username`, can never be left empty?

Show Answer

You add the `NOT NULL` constraint to the column definition when creating the table.

Added: Nov 30, 2025

120. How do you make sure that the `Price` column can only contain values greater than 0?

Show Answer

You use the `CHECK` constraint, like: `CHECK (Price > 0)`.

Added: Nov 30, 2025

121. How do you set a default value for a column, for example, setting `Country` to 'USA' if no country is specified?

Show Answer

You use the `DEFAULT` constraint, like: `Country VARCHAR(50) DEFAULT 'USA'`.

Added: Nov 30, 2025

122. Write a query to add a `CHECK` constraint to the existing `Employees` table to ensure `Age` is over 18.

Show Answer

ALTER TABLE Employees ADD CHECK (Age >= 18);

Added: Nov 30, 2025

123. Write a query to remove a column named `FaxNumber` from the `Suppliers` table.

Show Answer

ALTER TABLE Suppliers DROP COLUMN FaxNumber;

Added: Nov 30, 2025

124. Write a query to find products whose name ends with the letter 'S'.

Show Answer

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

Added: Nov 30, 2025

125. Write a query to find customers whose second letter in their name is 'a'.

Show Answer

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

Added: Nov 30, 2025

126. How do you make a new table called `ArchivedOrders` that has the exact same structure as the `Orders` table?

Show Answer

CREATE TABLE ArchivedOrders LIKE Orders;

Added: Nov 30, 2025

127. Write a query to insert a new order with `OrderID` 1005 and an `OrderDate` of today.

Show Answer

INSERT INTO Orders (OrderID, OrderDate) VALUES (1005, CURDATE());

Added: Nov 30, 2025

128. Write a query to copy all completed orders (Status = 'Completed') from `Orders` into `ArchivedOrders`.

Show Answer

INSERT INTO ArchivedOrders SELECT * FROM Orders WHERE Status = 'Completed';

Added: Nov 30, 2025

129. Write a query to find the `Name` of the product with the smallest ID.

Show Answer

SELECT ProductName FROM Products ORDER BY ProductID ASC LIMIT 1;

Added: Nov 30, 2025

130. Write a query to find the total length (number of characters) of all employee names combined.

Show Answer

SELECT SUM(LENGTH(Name)) FROM Employees;

Added: Nov 30, 2025

131. Write a query to convert all product names to uppercase.

Show Answer

SELECT UPPER(ProductName) FROM Products;

Added: Nov 30, 2025

132. Write a query to extract the first 3 letters of every customer's name.

Show Answer

SELECT SUBSTRING(Name, 1, 3) FROM Customers;

Added: Nov 30, 2025

133. Write a query to find all employees hired in the month of December.

Show Answer

SELECT Name, HireDate FROM Employees WHERE MONTH(HireDate) = 12;

Added: Nov 30, 2025

134. Write a query to find the year and month of the earliest order.

Show Answer

SELECT YEAR(MIN(OrderDate)), MONTH(MIN(OrderDate)) FROM Orders;

Added: Nov 30, 2025

135. Write a query to find the average age of all customers.

Show Answer

SELECT AVG(Age) FROM Customers;

Added: Nov 30, 2025

136. Write a query to count how many employees share each `JobTitle`.

Show Answer

SELECT JobTitle, COUNT(*) FROM Employees GROUP BY JobTitle;

Added: Nov 30, 2025

137. Write a query to find the customer who has the highest number of orders.

Show Answer

SELECT CustomerID, COUNT(*) AS OrderCount FROM Orders GROUP BY CustomerID ORDER BY OrderCount DESC LIMIT 1;

Added: Nov 30, 2025

138. Write a query to find the average price of products, excluding products that cost less than 10.

Show Answer

SELECT AVG(Price) FROM Products WHERE Price >= 10;

Added: Nov 30, 2025

139. Write a query to find the total value of products in the 'Beverages' category.

Show Answer

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

Added: Nov 30, 2025

140. Write a query to delete all records from the `TempTable` and reset the auto-increment counter.

Show Answer

TRUNCATE TABLE TempTable;

Added: Nov 30, 2025

141. What is a RIGHT JOIN?

Show Answer

A Right Join returns all rows from the right table and the matching rows from the left table. If there is no match in the left table, it returns NULL for the left table's columns.

Added: Nov 30, 2025

142. What is a FULL OUTER JOIN?

Show Answer

A Full Outer Join returns all rows when there is a match in one of the tables. It shows data even if one side has no matching record.

Added: Nov 30, 2025

143. Write a query to show all employees and the names of the departments they work in.

Show Answer

SELECT e.Name, d.DepartmentName FROM Employees e JOIN Departments d ON e.DepartmentID = d.DepartmentID;

Added: Nov 30, 2025

144. Write a query to find all customers and their country, ordered by country name.

Show Answer

SELECT Name, Country FROM Customers ORDER BY Country;

Added: Nov 30, 2025

145. Write a query to find the name of the product and the name of the supplier who provides it.

Show Answer

SELECT p.ProductName, s.SupplierName FROM Products p JOIN Suppliers s ON p.SupplierID = s.SupplierID;

Added: Nov 30, 2025

146. How do you find employees who manage themselves (self-join)?

Show Answer

SELECT e.Name FROM Employees e JOIN Employees m ON e.ManagerID = e.EmployeeID;

Added: Nov 30, 2025

147. Write a query to find all departments that currently have no employees.

Show Answer

SELECT d.DepartmentName FROM Departments d LEFT JOIN Employees e ON d.DepartmentID = e.DepartmentID WHERE e.EmployeeID IS NULL;

Added: Nov 30, 2025

148. What is a Schema in a database?

Show Answer

A Schema is a collection of logical data structures, or the overall blueprint, of the entire database. It defines the tables, columns, data types, indexes, and relationships.

Added: Nov 30, 2025

149. Write a query to get the `OrderID` for the order that has the highest `TotalAmount`.

Show Answer

SELECT OrderID FROM Orders ORDER BY TotalAmount DESC LIMIT 1;

Added: Nov 30, 2025

150. Write a query to find the number of employees whose salary is below the company average.

Show Answer

SELECT COUNT(*) FROM Employees WHERE Salary < (SELECT AVG(Salary) FROM Employees);

Added: Nov 30, 2025

151. Write a query to find the total number of orders placed in January of any year.

Show Answer

SELECT COUNT(*) FROM Orders WHERE MONTH(OrderDate) = 1;

Added: Nov 30, 2025

152. Write a query to find the employee name and their new salary after a 5% increase for everyone.

Show Answer

SELECT Name, Salary * 1.05 AS NewSalary FROM Employees;

Added: Nov 30, 2025

153. Write a query to update the `City` column for all customers from 'Paris' to 'Marseille'.

Show Answer

UPDATE Customers SET City = 'Marseille' WHERE City = 'Paris';

Added: Nov 30, 2025

154. Write a query to delete all products that belong to the category 'Discontinued'.

Show Answer

DELETE FROM Products WHERE Category = 'Discontinued';

Added: Nov 30, 2025

155. How do you combine two SELECT results and only show the common (intersecting) rows?

Show Answer

You use the `INTERSECT` operator, but you must ensure both SELECT statements have the same number of columns and data types.

Added: Nov 30, 2025

156. How do you combine two SELECT results and show rows from the first query that are not in the second (Set Difference)?

Show Answer

You use the `EXCEPT` or `MINUS` operator (depending on the database system).

Added: Nov 30, 2025

157. What is the purpose of the `UNION` operator?

Show Answer

The `UNION` operator is used to combine the result-set of two or more SELECT statements into a single result-set.

Added: Nov 30, 2025

158. Write a query to get the names of all employees and all customers in one list.

Show Answer

SELECT Name FROM Employees UNION SELECT Name FROM Customers;

Added: Nov 30, 2025

159. Write a query to find the name of the oldest employee.

Show Answer

SELECT Name FROM Employees ORDER BY Age DESC LIMIT 1;

Added: Nov 30, 2025

160. Write a query to find the most recent order date.

Show Answer

SELECT MAX(OrderDate) FROM Orders;

Added: Nov 30, 2025

161. Write a query to round the average product price to 2 decimal places.

Show Answer

SELECT ROUND(AVG(Price), 2) FROM Products;

Added: Nov 30, 2025

162. Write a query to find the absolute value (make it positive) of the number -15.

Show Answer

SELECT ABS(-15);

Added: Nov 30, 2025

163. Write a query to find the nearest integer less than or equal to 4.7.

Show Answer

SELECT FLOOR(4.7);

Added: Nov 30, 2025

164. Write a query to find the products whose names contain the word 'Big', anywhere in the name.

Show Answer

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

Added: Nov 30, 2025

165. Write a query to find employees whose names are shorter than 6 characters.

Show Answer

SELECT Name FROM Employees WHERE LENGTH(Name) < 6;

Added: Nov 30, 2025

166. Write a query to replace the word 'Old' with 'New' in the `Notes` column of the `Products` table.

Show Answer

UPDATE Products SET Notes = REPLACE(Notes, 'Old', 'New');

Added: Nov 30, 2025

167. Write a query to find the day of the month (1-31) for today's date.

Show Answer

SELECT DAYOFMONTH(CURDATE());

Added: Nov 30, 2025

168. Write a query to find all orders placed in the first half of the year (months 1-6).

Show Answer

SELECT * FROM Orders WHERE MONTH(OrderDate) BETWEEN 1 AND 6;

Added: Nov 30, 2025

169. Write a query to find all employees hired on the 1st day of any month.

Show Answer

SELECT Name FROM Employees WHERE DAYOFMONTH(HireDate) = 1;

Added: Nov 30, 2025

170. Write a query to count the number of products that are NOT in the 'Groceries' category.

Show Answer

SELECT COUNT(*) FROM Products WHERE Category <> 'Groceries';

Added: Nov 30, 2025

171. What is a transaction in SQL?

Show Answer

A transaction is a single unit of work in a database. It is a sequence of one or more SQL statements that are treated as a single, indivisible logical unit.

Added: Nov 30, 2025

172. What command starts a transaction?

Show Answer

The `START TRANSACTION` or `BEGIN` command starts a new transaction.

Added: Nov 30, 2025

173. Write a query to start a new database transaction.

Show Answer

START TRANSACTION;

Added: Nov 30, 2025

174. What is a self-join?

Show Answer

A self-join is when a table is joined to itself. This is often used to compare rows within the same table, like finding employees who work for the same manager.

Added: Nov 30, 2025

175. Write a query to find products whose price is an even number (divisible by 2 with no remainder).

Show Answer

SELECT ProductName FROM Products WHERE Price % 2 = 0;

Added: Nov 30, 2025

176. What is a Stored Procedure?

Show Answer

A Stored Procedure is a set of SQL statements that are stored in the database. It can be executed later by calling its name, which helps with security and performance.

Added: Nov 30, 2025

177. What is a Trigger?

Show Answer

A Trigger is a set of SQL code that automatically runs when a specific event occurs on a table, like an INSERT, UPDATE, or DELETE operation.

Added: Nov 30, 2025

178. Write a query to create an empty copy of the `Products` table, including all indexes and constraints, called `Products_Backup`.

Show Answer

CREATE TABLE Products_Backup AS SELECT * FROM Products WHERE 1=0;

Added: Nov 30, 2025

179. Write a query to find the second highest salary among all employees.

Show Answer

SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees);

Added: Nov 30, 2025

180. Write a query to find the top 10% of products by price.

Show Answer

SELECT ProductName, Price FROM Products ORDER BY Price DESC LIMIT (SELECT CEIL(COUNT(*) * 0.10) FROM Products);

Added: Nov 30, 2025

181. Write a query to find the number of different countries customers are from.

Show Answer

SELECT COUNT(DISTINCT Country) FROM Customers;

Added: Nov 30, 2025

182. Write a query to find the department with the highest average salary.

Show Answer

SELECT Department FROM Employees GROUP BY Department ORDER BY AVG(Salary) DESC LIMIT 1;

Added: Nov 30, 2025

183. Write a query to find the maximum price of a product for each supplier.

Show Answer

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

Added: Nov 30, 2025

184. Write a query to count the number of employees whose name starts with a vowel (A, E, I, O, U).

Show Answer

SELECT COUNT(*) FROM Employees WHERE Name LIKE 'A%' OR Name LIKE 'E%' OR Name LIKE 'I%' OR Name LIKE 'O%' OR Name LIKE 'U%';

Added: Nov 30, 2025

185. Write a query to find customers who placed an order in 2024.

Show Answer

SELECT Name FROM Customers WHERE CustomerID IN (SELECT DISTINCT CustomerID FROM Orders WHERE YEAR(OrderDate) = 2024);

Added: Nov 30, 2025

186. Write a query to find all orders placed on the last day of the month.

Show Answer

SELECT * FROM Orders WHERE DAY(OrderDate) = DAY(LAST_DAY(OrderDate));

Added: Nov 30, 2025

187. What is a composite primary key?

Show Answer

A composite primary key is a primary key that consists of two or more columns whose values, when combined, uniquely identify a row in a table.

Added: Nov 30, 2025

188. What is the purpose of the `AS` keyword?

Show Answer

The `AS` keyword is used to give a temporary new name (alias) to a table or a column in a query result, making the output easier to read.

Added: Nov 30, 2025

189. Write a query to find the `EmployeeID` and the total number of orders they processed (assuming a column `ProcessedBy` in `Orders`).

Show Answer

SELECT ProcessedBy, COUNT(OrderID) AS TotalOrders FROM Orders GROUP BY ProcessedBy;

Added: Nov 30, 2025

190. Write a query to find the earliest hire date among all employees.

Show Answer

SELECT MIN(HireDate) FROM Employees;

Added: Nov 30, 2025

191. Write a query to find products whose price is exactly divisible by 5.

Show Answer

SELECT ProductName FROM Products WHERE Price % 5 = 0;

Added: Nov 30, 2025

192. Write a query to find the third most expensive product.

Show Answer

SELECT ProductName, Price FROM Products ORDER BY Price DESC LIMIT 1 OFFSET 2;

Added: Nov 30, 2025

193. Write a query to find the average salary of employees who are managers.

Show Answer

SELECT AVG(Salary) FROM Employees WHERE JobTitle = 'Manager';

Added: Nov 30, 2025

194. Write a query to find customers whose ID is between 10 and 20 (exclusive).

Show Answer

SELECT * FROM Customers WHERE CustomerID > 10 AND CustomerID < 20;

Added: Nov 30, 2025

195. Write a query to find all employees who have a `NULL` value in the `Phone` column.

Show Answer

SELECT Name FROM Employees WHERE Phone IS NULL;

Added: Nov 30, 2025

196. What is the `ORDER BY` clause?

Show Answer

The `ORDER BY` clause is used to sort the result-set of a query in ascending (ASC) or descending (DESC) order based on one or more columns.

Added: Nov 30, 2025

197. Write a query to convert the `OrderDate` to the full month name (e.g., January).

Show Answer

SELECT DATE_FORMAT(OrderDate, '%M') FROM Orders;

Added: Nov 30, 2025

198. Write a query to get the number of years between today and the oldest employee's hire date.

Show Answer

SELECT DATEDIFF(CURDATE(), MIN(HireDate)) / 365 FROM Employees;

Added: Nov 30, 2025

199. Write a query to select a product name and its price, and sort by price (lowest to highest).

Show Answer

SELECT ProductName, Price FROM Products ORDER BY Price;

Added: Nov 30, 2025

200. Write a query to count the number of orders for each unique order status.

Show Answer

SELECT Status, COUNT(*) FROM Orders GROUP BY Status;

Added: Nov 30, 2025