SQL Quiz - Multiple Choice Questions (MCQ)

Welcome to the SQL quiz. This quiz consists of 40 multiple-choice questions to put your understanding of SQL concepts to the test. Each question comes with detailed explanations to help you learn and solidify your SQL expertise. Let's dive in and see how well you know your SQL!

1. SQL stands for? 

a) Structured Query Language 
b) Sequential Query Language 
c) Simple Query Language 
d) Solid Quality Language 

Answer: 

a) Structured Query Language 

Explanation: 

SQL stands for Structured Query Language. It's a standard language for managing and manipulating databases.

2. What does the acronym "CRUD" stand for in SQL? 

a) Create, Read, Update, Delete 
b) Columns, Rows, Update, Drop 
c) Concatenate, Retrieve, Union, Drop 
d) Copy, Run, Update, Display 

Answer:

a) Create, Read, Update, Delete 

Explanation: 

CRUD is an acronym that represents the four basic operations that can be performed on data in a database. It stands for Create (INSERT), Read (SELECT), Update (UPDATE), and Delete (DELETE). 

3. Which SQL command is used to retrieve data from a database? 

a) SEARCH 
b) RETRIEVE 
c) SELECT 
d) FIND 

Answer: 

c) SELECT 

Explanation: 

The SELECT statement is used to retrieve data from a database. It allows you to specify the columns you want to retrieve and the conditions that the data must meet. 

4. What is the purpose of the WHERE clause in SQL? 

a) It specifies the columns to be retrieved. 
b) It filters the rows returned by the SELECT statement. 
c) It orders the results in ascending or descending order. 
d) It creates a new table. 

Answer: 

b) It filters the rows returned by the SELECT statement. 

Explanation: 

The WHERE clause is used to filter the rows returned by the SELECT statement based on specified conditions. It allows you to retrieve only the data that meets the specified criteria. 

5. What is the purpose of the ORDER BY clause in SQL? 

a) It filters the rows returned by the SELECT statement. 
b) It specifies the columns to be retrieved. 
c) It creates a new table. 
d) It orders the results in ascending or descending order. 

Answer: 

d) It orders the results in ascending or descending order. 

Explanation: 

The ORDER BY clause is used to sort the results of a SELECT statement in either ascending or descending order based on one or more columns. 

6. Which SQL command is used to insert new data into a database table? 

a) ADD 
b) INSERT 
c) CREATE 
d) UPDATE 

Answer: 

b) INSERT 

Explanation: 

The INSERT statement is used to insert new data into a table. It allows you to add one or more rows to a database table. 

7. What does the acronym "DDL" stand for in SQL? 

a) Data Definition Language 
b) Data Deletion Language 
c) Data Display Language 
d) Data Description Language 

Answer: 

a) Data Definition Language

Explanation: 

DDL stands for Data Definition Language. It is used to define, manage, and modify the structure of a database, such as creating and altering tables. 

8. Which SQL command is used to update existing data in a database table? 

a) MODIFY 
b) ALTER 
c) UPDATE 
d) CHANGE

Answer: 

c) UPDATE 

Explanation: 

The UPDATE statement is used to modify existing data in a database table. It allows you to change the values of one or more columns in a specified row or set of rows. 

9. What is the purpose of the GROUP BY clause in SQL? 

a) It filters the rows returned by the SELECT statement. 
b) It groups rows with the same values into summary rows. 
c) It orders the results in ascending or descending order. 
d) It specifies the columns to be retrieved. 

Answer: 

b) It groups rows with the same values into summary rows. 

Explanation: 

The GROUP BY clause is used to group rows with the same values in one or more columns into summary rows. It is often used with aggregate functions like SUM or COUNT to calculate summary values for each group. 

10. What is the purpose of the HAVING clause in SQL? 

a) It orders the results in ascending or descending order. 
b) It filters the rows returned by the SELECT statement. 
c) It specifies the columns to be retrieved. 
d) It filters the summary rows created by the GROUP BY clause. 

Answer: 

d) It filters the summary rows created by the GROUP BY clause. 

Explanation: 

The HAVING clause is used to filter the summary rows created by the GROUP BY clause based on specified conditions. It is similar to the WHERE clause but is used with aggregate functions. 

11. What is the purpose of the DISTINCT keyword in SQL? 

a) It filters the rows returned by the SELECT statement. 
b) It specifies the columns to be retrieved. 
c) It removes duplicate rows from the result set. 
d) It orders the results in ascending or descending order. 

Answer: 

c) It removes duplicate rows from the result set. 

Explanation: 

The DISTINCT keyword is used to remove duplicate rows from the result set of a SELECT statement. It ensures that only unique rows are included in the output. 

12. What is the purpose of the JOIN clause in SQL? 

a) It filters the rows returned by the SELECT statement. 
b) It specifies the columns to be retrieved. 
c) It combines rows from two or more tables based on a related column. 
d) It orders the results in ascending or descending order. 

Answer: 

c) It combines rows from two or more tables based on a related column. 

Explanation: 

The JOIN clause is used to combine rows from two or more tables based on a related column. It allows you to retrieve data from multiple tables in a single query. 

13. Which SQL command is used to delete data from a database table?

a) TRUNCATE 
b) DROP 
c) REMOVE 
d) DELETE 

Answer: 

d) DELETE 

Explanation: 

The DELETE statement is used to delete rows from a database table. It allows you to remove one or more rows that meet a specified condition. 

14. What is the purpose of the LIMIT clause in SQL? 

a) It filters the rows returned by the SELECT statement. 
b) It orders the results in ascending or descending order. 
c) It groups rows with the same values into summary rows. 
d) It limits the number of rows returned by the SELECT statement. 

Answer: 

d) It limits the number of rows returned by the SELECT statement. 

Explanation: 

The LIMIT clause is used to restrict the number of rows returned by the SELECT statement to a specified number or range. 

15. What is the purpose of the BETWEEN operator in SQL? 

a) It filters the rows returned by the SELECT statement. 
b) It checks if a value exists in a specified list of values. 
c) It checks if a value falls within a specified range. 
d) It performs a pattern match on a string. 

Answer: 

c) It checks if a value falls within a specified range. 

Explanation: 

The BETWEEN operator is used to check if a value falls within a specified range of values, inclusive of the endpoints. 

16. What is the purpose of the LIKE operator in SQL? 

a) It checks if a value exists in a specified list of values. 
b) It filters the rows returned by the SELECT statement. 
c) It performs a pattern match on a string. 
d) It checks if a value falls within a specified range. 

Answer: 

c) It performs a pattern match on a string. 

Explanation: 

The LIKE operator is used for pattern matching in SQL. It allows you to match strings based on patterns that include wildcard characters like % and _. 

17. What does the COUNT function do in SQL? 

a) It counts the total number of rows in a table. 
b) It calculates the average value of a column. 
c) It retrieves the maximum value in a column. 
d) It counts the number of non-null values in a column. 

Answer: 

a) It counts the total number of rows in a table. 

Explanation: 

The COUNT function in SQL is used to count the total number of rows in a table. It can also be used with the GROUP BY clause to count the number of rows in each group. 

18. What does the SUM function do in SQL? 

a) It retrieves the maximum value in a column. 
b) It calculates the average value of a column. 
c) It counts the total number of rows in a table. 
d) It calculates the sum of values in a column. 

Answer: 

d) It calculates the sum of values in a column. 

Explanation: 

The SUM function in SQL is used to calculate the sum of numeric values in a column. 

19. What does the AVG function do in SQL? 

a) It calculates the sum of values in a column. 
b) It counts the total number of rows in a table. 
c) It retrieves the maximum value in a column. 
d) It calculates the average value of a column. 

Answer: 

d) It calculates the average value of a column. 

Explanation: 

The AVG function in SQL is used to calculate the average (mean) value of numeric values in a column. 

20. What does the MAX function do in SQL? 

a) It calculates the sum of values in a column. 
b) It counts the total number of rows in a table. 
c) It retrieves the minimum value in a column. 
d) It retrieves the maximum value in a column. 

Answer: 

d) It retrieves the maximum value in a column. 

Explanation: 

The MAX function in SQL is used to retrieve the maximum value from a column. 

21. What does the MIN function do in SQL? 

a) It retrieves the maximum value in a column. 
b) It calculates the sum of values in a column. 
c) It calculates the average value of a column. 
d) It retrieves the minimum value in a column. 

Answer: 

d) It retrieves the minimum value in a column. 

Explanation: 

The MIN function in SQL is used to retrieve the minimum value from a column. 

22. What is the purpose of the NULL value in SQL? 

a) It represents an empty string. 
b) It represents a missing or unknown value. 
c) It represents the absence of a primary key. 
d) It represents a zero value. 

Answer: 

b) It represents a missing or unknown value. 

Explanation: 

In SQL, the NULL value represents a missing or unknown value. It is different from an empty string or a zero value. 

23. What is the purpose of the UNION operator in SQL? 

a) It combines the results of two or more SELECT statements. 
b) It performs a pattern match on a string. 
c) It retrieves the maximum value in a column. 
d) It filters the rows returned by the SELECT statement. 

Answer: 

a) It combines the results of two or more SELECT statements. 

Explanation: 

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

24. What is the purpose of the INNER JOIN in SQL? 

a) It retrieves the maximum value in a column. 
b) It combines rows from two or more tables based on a related column. 
c) It filters the rows returned by the SELECT statement. 
d) It performs a pattern match on a string. 

Answer: 

b) It combines rows from two or more tables based on a related column. 

Explanation: 

The INNER JOIN in SQL is used to combine rows from two or more tables based on a related column. It returns only the rows that have matching values in both tables. 

25. What is the purpose of the LEFT JOIN in SQL? 

a) It combines rows from two or more tables based on a related column. 
b) It retrieves the maximum value in a column. 
c) It filters the rows returned by the SELECT statement. 
d) It performs a pattern match on a string. 

Answer: 

a) It combines rows from two or more tables based on a related column. 

Explanation: 

The LEFT JOIN in SQL is used to combine rows from two or more tables based on a related column. It returns all the rows from the left table and the matching rows from the right table. If there is no match, NULL values are returned for the right table's columns.

26. Which of the following can be used to create a relationship between two tables? 

a) FOREIGN KEY 
b) DISTINCT KEY 
c) PRIMARY KEY 
d) UNIQUE KEY 

Answer: 

a) FOREIGN KEY 

Explanation: 

A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table containing the foreign key is called the child table, and the table containing the primary key is called the referenced or parent table.

27. Which of the following can be used to uniquely identify each row of the table?

a) FOREIGN KEY 
b) DISTINCT KEY 
c) PRIMARY KEY 
d) UNIQUE KEY 

Answer: 

c) PRIMARY KEY 

Explanation: 

A PRIMARY KEY in SQL is a column (or a combination of columns) in a table that uniquely identifies each row of the table.

Quiz on SQL Queries

Let's create a sample database table named "Employees" with columns "ID," "Name," "Age," and "Department." We will also insert some rows of data into the table.
-- Creating the Employees table
CREATE TABLE Employees (
  ID INT PRIMARY KEY,
  Name VARCHAR(50),
  Age INT,
  Department VARCHAR(50)
);

-- Inserting data into the Employees table
INSERT INTO Employees (ID, Name, Age, Department)
VALUES
  (1, 'John Doe', 30, 'HR'),
  (2, 'Jane Smith', 28, 'Finance'),
  (3, 'Michael Johnson', 35, 'IT'),
  (4, 'Emily Williams', 32, 'Sales'),
  (5, 'Robert Brown', 27, 'Marketing');

28. What SQL query is used to retrieve all columns from the Employees table? 

a) SELECT * FROM Employees;
b) SELECT Name, Age, Department FROM Employees;
c) SELECT ID, Name, Age, Department FROM Employees;
d) SELECT Employees;

Answer: 

a) SELECT * FROM Employees; 

Explanation: 

Option (a) retrieves all columns from the Employees table using the wildcard (*) symbol to select all columns. 

29. How do you filter the Employees table to show only the rows where the Age is greater than 30? 

a) SELECT * FROM Employees WHERE Age > 30; 
b) SELECT * FROM Employees HAVING Age > 30; 
c) SELECT * FROM Employees WHERE Age >= 30; 
d) SELECT * FROM Employees FILTER BY Age > 30; 

Answer: 

a) SELECT * FROM Employees WHERE Age > 30; 

Explanation: 

Option (a) uses the WHERE clause to filter the rows where the Age column is greater than 30. 

30. What SQL query is used to count the number of employees in each department? 

a) SELECT COUNT() FROM Employees GROUP BY Department; 
b) SELECT COUNT() AS TotalEmployees FROM Employees GROUP BY Department; 
c) SELECT COUNT(DISTINCT Department) FROM Employees; 
d) SELECT COUNT(Department) FROM Employees GROUP BY Department; 

Answer: 

a) SELECT COUNT(*) FROM Employees GROUP BY Department; 

Explanation: 

Option (a) uses the COUNT(*) function along with the GROUP BY clause to count the number of employees in each department. 

31. How do you update the age of an employee with ID 3 to 36 years old? 

a) UPDATE Employees SET Age = 36 WHERE ID = 3; 
b) UPDATE Employees AGE = 36 WHERE ID = 3; 
c) UPDATE Employees VALUES (3, 'Michael Johnson, 36, 'IT'); 
d) MODIFY Employees SET Age = 36 WHERE ID = 3; 

Answer: 

a) UPDATE Employees SET Age = 36 WHERE ID = 3; 

Explanation: 

Option (a) uses the UPDATE statement to change the Age column to 36 for the employee with ID 3.

32. How do you delete the employee with ID 5 from the Employees table? 

a) DELETE FROM Employees WHERE ID = 5; 
b) DELETE * FROM Employees WHERE ID = 5; 
c) REMOVE FROM Employees WHERE ID = 5; 
d) DROP FROM Employees WHERE ID = 5; 

Answer: 

a) DELETE FROM Employees WHERE ID = 5; 

Explanation: 

Option (a) uses the DELETE statement to remove the row where the ID column is equal to 5. 

33. How do you retrieve employees with the name 'John Doe' or 'Emily Williams'? 

a) SELECT * FROM Employees WHERE Name = 'John Doe' OR 'Emily Williams'; 
b) SELECT * FROM Employees WHERE Name = 'John Doe' AND 'Emily Williams'; 
c) SELECT * FROM Employees WHERE Name = 'John Doe', 'Emily Williams'; 
d) SELECT * FROM Employees WHERE Name IN ('John Doe', 'Emily Williams'); 

Answer: 

d) SELECT * FROM Employees WHERE Name IN ('John Doe', 'Emily Williams'); 

Explanation: 

Option (d) uses the IN operator to retrieve employees with the name 'John Doe' or 'Emily Williams'.

34. What SQL query is used to retrieve the oldest employee's name and age? 

a) SELECT Name, Age FROM Employees ORDER BY Age DESC LIMIT 1; 
b) SELECT MAX(Age), Name FROM Employees; 
c) SELECT TOP 1 Name, Age FROM Employees ORDER BY Age DESC; 
d) SELECT Name, Age FROM Employees WHERE Age = MAX(Age); 

Answer: 

a) SELECT Name, Age FROM Employees ORDER BY Age DESC LIMIT 1; 

Explanation: 

Option (a) retrieves the Name and Age columns from the Employees table, orders the result by Age in descending order, and selects only the first row using the LIMIT 1 clause. 

35. What SQL query is used to retrieve the average age of employees? 

a) SELECT AVG(Age) FROM Employees; 
b) SELECT AVERAGE(Age) FROM Employees; 
c) SELECT SUM(Age)/COUNT(*) FROM Employees; 
d) SELECT AGE(AVG) FROM Employees; 

Answer: 

a) SELECT AVG(Age) FROM Employees; 

Explanation: 

Option (a) uses the AVG() function to calculate the average age of employees. 

36. How do you retrieve the youngest employee's name and age? 

a) SELECT Name, Age FROM Employees ORDER BY Age ASC LIMIT 1; 
b) SELECT MIN(Age), Name FROM Employees; 
c) SELECT LAST(Name), Age FROM Employees ORDER BY Age DESC; 
d) SELECT Name, Age FROM Employees WHERE Age = MIN(Age); 

Answer: 

a) SELECT Name, Age FROM Employees ORDER BY Age ASC LIMIT 1; 

Explanation: 

Option (a) retrieves the Name and Age columns from the Employees table, orders the result by Age in ascending order, and selects only the first row using the LIMIT 1 clause.

Conclusion 

Congratulations on completing the SQL quiz! We hope you enjoyed the challenge and learned some new SQL concepts along the way. SQL is a powerful language used for managing and manipulating databases. Keep practicing and experimenting to become an SQL expert! Happy querying!

Comments