SQL Multiple-Choice Questions
WHERE Clause
Which SQL query retrieves employees earning more than $50,000?
a) SELECT * FROM Employees WHERE salary > 50000;
b) SELECT * FROM Employees HAVING salary > 50000;
c) SELECT * FROM Employees GROUP BY salary > 50000;
d) SELECT * FROM Employees ORDER BY salary > 50000;
**Answer:** a) SELECT * FROM Employees WHERE salary > 50000;
Arithmetic Operators
What will be the output of SELECT 10 + 5 * 2;?
a) 30
b) 20
c) 25
d) 15
**Answer:** b) 20
Logical Operators
Which of the following correctly filters records where age is between 20 and 30?
a) SELECT * FROM Students WHERE age > 20 OR age < 30;
b) SELECT * FROM Students WHERE age > 20 AND age < 30;
c) SELECT * FROM Students WHERE age BETWEEN 20 AND 30;
d) Both b and c
**Answer:** d) Both b and c
Date Functions
Which function returns the current date?
a) GETDATE()
b) CURRDATE()
c) NOW()
d) Both a and c
**Answer:** d) Both a and c
Numeric Functions
What does ROUND(23.6789, 2) return?
a) 23.67
b) 23.68
c) 23.679
d) 23.6
**Answer:** b) 23.68
String Conversion
Which function converts a number to a string in SQL?
a) CAST()
b) TO_CHAR()
c) CONVERT()
d) All of the above
**Answer:** d) All of the above
Creating Tables with Relationships
Which SQL command ensures a column in one table references another table's primary key?
a) CHECK
b) FOREIGN KEY
c) UNIQUE
d) INDEX
**Answer:** b) FOREIGN KEY
Key and Integrity Constraints
Which constraint ensures that all values in a column are unique and not NULL?
a) NOT NULL
b) PRIMARY KEY
c) UNIQUE
d) FOREIGN KEY
**Answer:** b) PRIMARY KEY
Nested Queries
Which of the following uses a nested query correctly?
a) SELECT name FROM Employees WHERE id = (SELECT manager_id FROM Managers);
b) SELECT name FROM Employees WHERE (SELECT manager_id FROM Managers) = id;
c) SELECT name FROM Employees (SELECT manager_id FROM Managers WHERE id =
Employees.id);
d) None of the above
**Answer:** a) SELECT name FROM Employees WHERE id = (SELECT manager_id FROM
Managers);
Subqueries
What is the result of using a subquery in the FROM clause?
a) A table
b) A column
c) A single value
d) An error
**Answer:** a) A table
Grouping and Aggregation
Which query returns the total salary of each department?
a) SELECT department, SUM(salary) FROM Employees GROUP BY department;
b) SELECT department, COUNT(salary) FROM Employees GROUP BY department;
c) SELECT department, AVG(salary) FROM Employees GROUP BY department;
d) SELECT department, salary FROM Employees GROUP BY department;
**Answer:** a) SELECT department, SUM(salary) FROM Employees GROUP BY department;
Ordering
How do you sort employees by salary in descending order?
a) SELECT * FROM Employees ORDER BY salary ASC;
b) SELECT * FROM Employees ORDER BY salary;
c) SELECT * FROM Employees ORDER BY salary DESC;
d) SELECT * FROM Employees SORT salary DESC;
**Answer:** c) SELECT * FROM Employees ORDER BY salary DESC;
Joins
Which join retrieves only matching rows from two tables?
a) INNER JOIN
b) LEFT JOIN
c) RIGHT JOIN
d) FULL JOIN
**Answer:** a) INNER JOIN
Updatable Views
Which SQL condition allows an updatable view?
a) View must have a GROUP BY clause
b) View must include a JOIN
c) View must be based on a single table without aggregation
d) View must contain an ORDER BY clause
**Answer:** c) View must be based on a single table without aggregation
Non-Updatable Views
Which SQL statement creates a non-updatable view?
a) CREATE VIEW v1 AS SELECT id, name FROM Employees;
b) CREATE VIEW v2 AS SELECT department, COUNT(*) FROM Employees GROUP BY
department;
c) CREATE VIEW v3 AS SELECT * FROM Employees WHERE salary > 50000;
d) CREATE VIEW v4 AS SELECT * FROM Employees;
**Answer:** b) CREATE VIEW v2 AS SELECT department, COUNT(*) FROM Employees
GROUP BY department;
Relational Set Operations
Which SQL operator combines two queries without duplicates?
a) UNION ALL
b) UNION
c) INTERSECT
d) EXCEPT
**Answer:** b) UNION
EXISTS Clause
What does EXISTS check in a query?
a) If a subquery returns any rows
b) If a column exists
c) If a row exists in a table
d) If a database exists
**Answer:** a) If a subquery returns any rows
CASE Statement
Which SQL statement correctly uses CASE?
a) SELECT name, CASE WHEN age > 18 THEN 'Adult' ELSE 'Minor' END FROM Students;
b) SELECT name, CASE WHEN age > 18 'Adult' ELSE 'Minor' END FROM Students;
c) SELECT name, CASE age > 18 THEN 'Adult' ELSE 'Minor' END FROM Students;
d) SELECT name, CASE age > 18 'Adult' ELSE 'Minor' END FROM Students;
**Answer:** a) SELECT name, CASE WHEN age > 18 THEN 'Adult' ELSE 'Minor' END FROM
Students;
HAVING vs WHERE
Which clause is used with aggregate functions?
a) WHERE
b) HAVING
c) GROUP BY
d) ORDER BY
**Answer:** b) HAVING
Self-Join
What does a self-join do?
a) Joins a table with itself
b) Joins two different tables
c) Filters duplicate records
d) Returns NULL values
**Answer:** a) Joins a table with itself