DBMS and SQL questions
Keys, normalisation and transactions, plus SQL queries on sample tables: joins, subqueries, GROUP BY/HAVING, UNION and window functions.
1. DBMS stands for:
- Database Management System
- Digital Base Memory System
- Data Backup Management Software
- Database Machine Server
Answer
A. Database Management System Software that stores and manages data.
2. A primary key:
- Links to another table only
- Uniquely identifies each row and cannot be NULL
- Is always a text column
- Can repeat across rows
Answer
B. Uniquely identifies each row and cannot be NULL Primary keys are unique and not null.
3. A foreign key:
- Is the same as an index
- Must be unique in its own table
- Refers to the primary key of another table
- Cannot be NULL ever
Answer
C. Refers to the primary key of another table It enforces relationships between tables.
4. Which statement can remove only selected rows using a WHERE clause?
- DROP
- TRUNCATE
- DELETE
- ALTER
Answer
C. DELETE DELETE supports WHERE; TRUNCATE always empties the whole table and DROP removes the table.
5. Which clause filters groups after GROUP BY?
- LIMIT
- WHERE
- HAVING
- ORDER BY
Answer
C. HAVING WHERE filters rows before grouping; HAVING filters groups.
6. Which join returns only rows with matching values in both tables?
- INNER JOIN
- FULL OUTER JOIN
- CROSS JOIN
- LEFT JOIN
Answer
A. INNER JOIN INNER JOIN keeps matches only.
7. A LEFT JOIN returns:
- Only unmatched rows
- Only matching rows
- All rows from the right table
- All rows from the left table and matching rows from the right
Answer
D. All rows from the left table and matching rows from the right Unmatched right-side columns become NULL.
8. Which SQL keyword removes duplicate rows from a result?
- DISTINCT
- SINGLE
- UNIQUE
- DIFFERENT
Answer
A. DISTINCT SELECT DISTINCT removes duplicates.
9. Normalisation is done to:
- Add duplicate data
- Encrypt the database
- Make queries slower
- Reduce data redundancy and update anomalies
Answer
D. Reduce data redundancy and update anomalies It organises tables to avoid repeated data.
10. A table is in 1NF when:
- It has no primary key
- It has no foreign keys
- It has only one column
- Every column holds atomic (indivisible) values
Answer
D. Every column holds atomic (indivisible) values 1NF forbids repeating groups and multi-valued cells.
11. ACID properties of transactions are:
- Access, Control, Index, Data
- Accuracy, Clarity, Integrity, Design
- Atomicity, Consistency, Isolation, Durability
- Atomicity, Concurrency, Indexing, Durability
Answer
C. Atomicity, Consistency, Isolation, Durability ACID guarantees reliable transactions.
12. "Atomicity" means a transaction:
- Uses one table only
- Runs very fast
- Is visible to all users at once
- Happens completely or not at all
Answer
D. Happens completely or not at all Partial transactions are rolled back.
13. Which command saves the changes of a transaction permanently?
- GRANT
- ROLLBACK
- SAVE
- COMMIT
Answer
D. COMMIT COMMIT makes changes permanent.
14. An index on a column mainly helps to:
- Encrypt the data
- Reduce the table size
- Prevent all duplicates
- Speed up searches on that column
Answer
D. Speed up searches on that column Indexes make lookups faster at the cost of extra storage and slower writes.
15. Which SQL function counts rows?
- NUMBER()
- TOTAL()
- COUNT()
- SUM()
Answer
C. COUNT() COUNT(*) counts rows.
16. Which is a DDL command?
- UPDATE
- CREATE
- SELECT
- INSERT
Answer
B. CREATE DDL defines structure: CREATE, ALTER, DROP.
17. Which is a DML command?
- DROP
- ALTER
- UPDATE
- CREATE
Answer
C. UPDATE DML changes data: INSERT, UPDATE, DELETE.
18. Table Emp has salaries 30, 50, 50, 70 (in thousands). What does SELECT COUNT(DISTINCT salary) FROM Emp return?
- 200
- 4
- 3
- 2
Answer
C. 3 Distinct salaries are 30, 50 and 70.
19. Table Marks has values 40, 60, 80. What does SELECT AVG(score) FROM Marks return?
- 60
- 80
- 40
- 180
Answer
A. 60 (40 + 60 + 80)/3 = 60.
20. Table T has rows with dept values A, A, B, C, C, C. What does SELECT dept, COUNT(*) FROM T GROUP BY dept HAVING COUNT(*) > 1 return?
- A 2 and C 3
- C 3 only
- A 2, B 1 and C 3
- B 1 only
Answer
A. A 2 and C 3 Only groups with more than one row remain.
21. Which query returns the second highest salary from Emp(salary)?
- SELECT MAX(salary) - 1 FROM Emp
- SELECT MIN(salary) FROM Emp
- SELECT salary FROM Emp ORDER BY salary
- SELECT MAX(salary) FROM Emp WHERE salary < (SELECT MAX(salary) FROM Emp)
Answer
D. SELECT MAX(salary) FROM Emp WHERE salary < (SELECT MAX(salary) FROM Emp) The highest salary below the overall maximum is the second highest.
22. A view in SQL is:
- A backup of a table
- A virtual table based on a query
- An index
- A user account
Answer
B. A virtual table based on a query Views store a query, not the data itself (unless materialised).
23. What does the LIKE 'A%' condition match?
- Values ending with A
- Exactly the value "A%"
- Values containing only A
- Values starting with A
Answer
D. Values starting with A % matches any sequence of characters.
24. Which key can be chosen as the primary key from several unique candidates?
- Foreign key
- Candidate key
- Composite index
- Super index
Answer
B. Candidate key Each candidate key could serve as the primary key.
25. An ER diagram represents:
- Network routes
- Entities, their attributes and relationships
- CPU scheduling
- Program flow
Answer
B. Entities, their attributes and relationships Entity-Relationship diagrams model data.
26. Which SQL statement adds a new column to an existing table?
- INSERT COLUMN
- ALTER TABLE … ADD
- MODIFY TABLE … NEW
- UPDATE TABLE … ADD
Answer
B. ALTER TABLE … ADD ALTER TABLE changes structure.
27. A deadlock occurs when:
- Two transactions wait for each other's locks forever
- An index is missing
- The database is full
- A query has a syntax error
Answer
A. Two transactions wait for each other's locks forever Each holds a lock the other needs.
28. NoSQL databases such as MongoDB typically store data as:
- Spreadsheets
- Documents (e.g. JSON-like)
- Binary trees on paper
- Only fixed-schema tables
Answer
B. Documents (e.g. JSON-like) Many NoSQL stores are document- or key-value-based.
29. Which ordering does ORDER BY use by default?
- Random
- Insertion order
- Descending
- Ascending
Answer
D. Ascending ASC is the default.
30. Which of these removes a table completely, including its structure?
- CLEAR TABLE
- DROP TABLE
- DELETE FROM
- TRUNCATE TABLE
Answer
B. DROP TABLE DROP deletes the table definition and data.
31. How many rows does this INNER JOIN return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT COUNT(*)
FROM employees e
JOIN departments d ON e.dept_id = d.id;- 4
- 3
- 5
- 2
Answer
B. 3 Only Asha, Ravi and Neha have a dept_id that exists in departments (10, 20). Kiran (30) and Imran (NULL) have no match.
32. How many rows does this LEFT JOIN return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT COUNT(*)
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.id;- 5
- 6
- 4
- 3
Answer
A. 5 A LEFT JOIN keeps every employee (5). Unmatched ones get NULL department columns.
33. What does this query return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT e.name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.id
WHERE d.id IS NULL
ORDER BY e.name;- Kiran
- Imran, Kiran
- Sales
- Imran
Answer
B. Imran, Kiran LEFT JOIN + "IS NULL" finds employees with no matching department: Kiran (dept 30 does not exist) and Imran (NULL).
34. What does this RIGHT JOIN return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT d.dname
FROM employees e
RIGHT JOIN departments d ON e.dept_id = d.id
WHERE e.id IS NULL;- Sales
- HR
- IT
- NULL
Answer
A. Sales A RIGHT JOIN keeps every department. Only Sales (40) has no employee, so e.id is NULL for it.
35. What is the output?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT d.dname, COUNT(e.id)
FROM departments d
LEFT JOIN employees e ON e.dept_id = d.id
GROUP BY d.dname
ORDER BY d.dname;- HR 2, IT 1
- HR 2, IT 1, Sales 0
- HR 2, IT 1, Sales 1
- HR 1, IT 1, Sales 0
Answer
B. HR 2, IT 1, Sales 0 The LEFT JOIN keeps Sales with a NULL employee; COUNT(e.id) skips NULLs, so Sales shows 0. COUNT(*) would have shown 1.
36. How many rows does this FULL OUTER JOIN return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT COUNT(*)
FROM employees e
FULL JOIN departments d ON e.dept_id = d.id;- 5
- 8
- 6
- 7
Answer
C. 6 3 matched rows + 2 employees with no department (Kiran, Imran) + 1 department with no employee (Sales) = 6.
37. How many rows does this CROSS JOIN return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT COUNT(*)
FROM employees
CROSS JOIN departments;- 15
- 8
- 3
- 5
Answer
A. 15 A cross join pairs every row with every row: 5 × 3 = 15.
38. Which employees does this self join return?
-- employees
-- id | name | dept_id | salary | manager_id
-- 1 | Asha | 10 | 50000 | NULL
-- 2 | Ravi | 20 | 40000 | 1
-- 3 | Neha | 10 | 60000 | 1
-- 4 | Kiran | 30 | 35000 | 3
-- 5 | Imran | NULL | 45000 | 3
SELECT e.name
FROM employees e
JOIN employees m ON e.manager_id = m.id
WHERE m.name = 'Neha'
ORDER BY e.name;- Neha
- Asha, Ravi
- Imran, Kiran
- Ravi, Neha
Answer
C. Imran, Kiran Neha has id 3. Kiran and Imran have manager_id 3.
39. What is the output?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT e.name, d.dname
FROM employees e
JOIN departments d ON e.dept_id = d.id
WHERE e.salary > 45000
ORDER BY e.name;- Neha HR
- Asha HR, Ravi IT, Neha HR
- Asha HR, Neha HR, Imran NULL
- Asha HR, Neha HR
Answer
D. Asha HR, Neha HR Joined rows are Asha, Ravi, Neha; of these only Asha (50000) and Neha (60000) earn more than 45000.
40. What does this self join return?
-- employees
-- id | name | dept_id | salary | manager_id
-- 1 | Asha | 10 | 50000 | NULL
-- 2 | Ravi | 20 | 40000 | 1
-- 3 | Neha | 10 | 60000 | 1
-- 4 | Kiran | 30 | 35000 | 3
-- 5 | Imran | NULL | 45000 | 3
SELECT e.name, m.name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id
WHERE e.salary < 45000
ORDER BY e.name;- Kiran Asha, Ravi Neha
- Ravi Asha
- Kiran Neha, Ravi Asha
- Kiran NULL, Ravi Asha
Answer
C. Kiran Neha, Ravi Asha Ravi (40000) reports to id 1 (Asha); Kiran (35000) reports to id 3 (Neha).
41. Which departments have no employees?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT dname
FROM departments d
WHERE NOT EXISTS (
SELECT 1 FROM employees e WHERE e.dept_id = d.id
);- Sales
- IT
- HR
- HR, IT
Answer
A. Sales NOT EXISTS keeps a department only when no employee row matches it.
42. What is the output?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
-- departments
-- id | dname
-- 10 | HR
-- 20 | IT
-- 40 | Sales
SELECT name
FROM employees
WHERE dept_id IN (
SELECT id FROM departments WHERE dname IN ('HR', 'IT')
)
ORDER BY name;- Asha, Neha, Ravi, Kiran
- Asha, Neha, Ravi
- Asha, Neha
- Ravi
Answer
B. Asha, Neha, Ravi The subquery gives ids 10 and 20; employees in those are Asha, Neha (10) and Ravi (20).
43. What does this query return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
SELECT name
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);- Asha
- 60000
- Imran
- Neha
Answer
D. Neha The highest salary is 60000, which is Neha's.
44. What is the second highest salary returned here?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);- 60000
- 50000
- 40000
- 45000
Answer
B. 50000 Excluding the top salary (60000), the highest remaining is 50000. A classic interview query.
45. Who earns more than the average salary?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
SELECT name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees)
ORDER BY name;- Neha
- Asha, Imran, Neha
- Asha, Neha
- Imran, Kiran, Ravi
Answer
C. Asha, Neha Average = 230000 / 5 = 46000. Only Asha (50000) and Neha (60000) are above it.
46. What is the output?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
SELECT dept_id, SUM(salary)
FROM employees
WHERE dept_id IS NOT NULL
GROUP BY dept_id
ORDER BY dept_id;- 10 110000, 20 40000, 30 35000
- 10 50000, 20 40000, 30 35000
- 10 55000, 20 40000, 30 35000
- 10 110000, 20 40000, 30 35000, NULL 45000
Answer
A. 10 110000, 20 40000, 30 35000 Dept 10 has Asha + Neha = 110000. Imran (NULL) is filtered out by the WHERE clause.
47. What is the output?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
SELECT COUNT(dept_id)
FROM employees;- 3
- 4
- 0
- 5
Answer
B. 4 COUNT(column) skips NULLs. Imran's dept_id is NULL, so 4. COUNT(*) would give 5.
48. What is the output?
-- employees
-- id | name | dept_id | salary | manager_id
-- 1 | Asha | 10 | 50000 | NULL
-- 2 | Ravi | 20 | 40000 | 1
-- 3 | Neha | 10 | 60000 | 1
-- 4 | Kiran | 30 | 35000 | 3
-- 5 | Imran | NULL | 45000 | 3
SELECT COUNT(DISTINCT manager_id)
FROM employees;- 4
- 5
- 3
- 2
Answer
D. 2 manager_id values are NULL, 1, 1, 3, 3. Distinct non-NULL values: 1 and 3.
49. What does this query return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
SELECT COUNT(*)
FROM employees
WHERE dept_id = NULL;- 0
- 5
- 4
- 1
Answer
A. 0 Any comparison with NULL using "=" is unknown, so no row matches. Use "dept_id IS NULL" instead.
50. What does this return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
SELECT name
FROM employees
ORDER BY salary DESC
LIMIT 2;- Asha, Neha
- Kiran, Ravi
- Neha, Imran
- Neha, Asha
Answer
D. Neha, Asha Sorted high to low: Neha 60000, Asha 50000, … LIMIT 2 keeps the first two.
51. What does this return?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
SELECT name
FROM employees
ORDER BY salary
LIMIT 1 OFFSET 1;- Asha
- Kiran
- Imran
- Ravi
Answer
D. Ravi Ascending: Kiran 35000, Ravi 40000, … OFFSET 1 skips Kiran, LIMIT 1 takes Ravi.
52. What is the output after these statements?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
UPDATE employees
SET salary = salary + 5000
WHERE dept_id = 10;
SELECT SUM(salary) FROM employees;- 230000
- 235000
- 240000
- 255000
Answer
C. 240000 The total starts at 230000. Two employees in dept 10 get +5000 each: 240000.
53. What is the output after these statements?
-- employees
-- id | name | dept_id | salary
-- 1 | Asha | 10 | 50000
-- 2 | Ravi | 20 | 40000
-- 3 | Neha | 10 | 60000
-- 4 | Kiran | 30 | 35000
-- 5 | Imran | NULL | 45000
DELETE FROM employees
WHERE salary < 45000;
SELECT COUNT(*) FROM employees;- 5
- 3
- 2
- 4
Answer
B. 3 Ravi (40000) and Kiran (35000) are deleted; 3 rows remain. Imran (45000) is not less than 45000.
54. What is the output?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT name
FROM students
WHERE city = 'Hyd' AND marks > 70;- Ali
- Ali, Sana
- Ravi
- Ali, Ravi
Answer
A. Ali Hyderabad students are Ali (78) and Ravi (65); only Ali has more than 70.
55. What is the output?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT name
FROM students
ORDER BY marks DESC, name
LIMIT 2;- Meera, Ali
- Sana, Meera
- John, Meera
- Meera, Sana
Answer
D. Meera, Sana Meera and Sana tie on 92, so the second sort key (name) puts Meera first. In SQLite, NULL sorts last in DESC order.
56. What is the output?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT COUNT(DISTINCT city)
FROM students;- 5
- 4
- 2
- 3
Answer
D. 3 Cities: Hyd, Pune, Delhi.
57. What is the output?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT AVG(marks)
FROM students;- 92
- 78
- 81.75
- 65.4
Answer
C. 81.75 AVG ignores NULL: (78 + 92 + 65 + 92) / 4 = 81.75, not divided by 5.
58. What is the output?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT city, COUNT(*)
FROM students
GROUP BY city
ORDER BY city;- Delhi 1, Hyd 2, Pune 1
- Delhi 1, Hyd 2, Pune 2
- Hyd 2, Pune 2
- Delhi 1, Hyd 1, Pune 2
Answer
B. Delhi 1, Hyd 2, Pune 2 COUNT(*) counts rows, including John whose marks are NULL.
59. Which names does this LIKE pattern match?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT name
FROM students
WHERE name LIKE '%a'
ORDER BY name;- Meera, Ravi, Sana
- Ali, Sana
- Sana
- Meera, Sana
Answer
D. Meera, Sana "%a" means any characters followed by a final "a": Meera and Sana.
60. What is the output?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT COUNT(*)
FROM students
WHERE marks BETWEEN 65 AND 78;- 0
- 2
- 3
- 1
Answer
B. 2 BETWEEN includes both ends: Ravi (65) and Ali (78).
61. What is the output?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT name
FROM students
WHERE marks IS NULL;- No rows
- John, Sana
- NULL
- John
Answer
D. John Only John has no marks.
62. What is the output?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT COUNT(*)
FROM students
WHERE marks = (SELECT MAX(marks) FROM students);- 1
- 92
- 2
- 5
Answer
C. 2 The maximum is 92 and two students (Sana, Meera) have it.
63. What is the output?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT name, COALESCE(marks, 0)
FROM students
WHERE city = 'Pune'
ORDER BY name;- John 92, Sana 0
- John NULL, Sana 92
- John 0, Sana 92
- Sana 92
Answer
C. John 0, Sana 92 COALESCE returns the first non-NULL value, so John's NULL becomes 0.
64. Who is marked "Fail" by this CASE expression?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT name
FROM students
WHERE CASE WHEN marks >= 75 THEN 'Pass'
ELSE 'Fail' END = 'Fail'
ORDER BY name;- Ravi
- Ali, Ravi
- John, Ravi
- John
Answer
C. John, Ravi Ravi (65) fails. For John, NULL >= 75 is not true, so CASE falls to ELSE and he is also "Fail".
65. What is the output of this window function?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT name, RANK() OVER (ORDER BY marks DESC)
FROM students
WHERE marks IS NOT NULL
ORDER BY 2, name;- Meera 1, Sana 1, Ali 2, Ravi 3
- Meera 1, Sana 2, Ali 3, Ravi 4
- Ali 1, Meera 2, Ravi 3, Sana 4
- Meera 1, Sana 1, Ali 3, Ravi 4
Answer
D. Meera 1, Sana 1, Ali 3, Ravi 4 RANK gives ties the same rank and then skips: 1, 1, 3, 4.
66. What is the output of this window function?
-- students
-- id | name | marks | city
-- 1 | Ali | 78 | Hyd
-- 2 | Sana | 92 | Pune
-- 3 | Ravi | 65 | Hyd
-- 4 | Meera | 92 | Delhi
-- 5 | John | NULL | Pune
SELECT name, DENSE_RANK() OVER (ORDER BY marks DESC)
FROM students
WHERE marks IS NOT NULL
ORDER BY 2, name;- Meera 1, Sana 1, Ali 3, Ravi 4
- Meera 1, Sana 1, Ali 2, Ravi 3
- Meera 1, Sana 1, Ali 2, Ravi 4
- Meera 1, Sana 2, Ali 3, Ravi 4
Answer
B. Meera 1, Sana 1, Ali 2, Ravi 3 DENSE_RANK gives ties the same rank without gaps: 1, 1, 2, 3.
67. Which customers does this return?
-- orders
-- id | customer | amount
-- 1 | Amit | 500
-- 2 | Bina | 300
-- 3 | Amit | 700
-- 4 | Chen | 200
-- 5 | Bina | 400
-- 6 | Amit | 100
SELECT customer
FROM orders
GROUP BY customer
HAVING SUM(amount) > 600
ORDER BY customer;- Bina
- Amit, Bina, Chen
- Amit
- Amit, Bina
Answer
D. Amit, Bina Totals: Amit 1300, Bina 700, Chen 200. HAVING keeps groups above 600.
68. What is the output?
-- orders
-- id | customer | amount
-- 1 | Amit | 500
-- 2 | Bina | 300
-- 3 | Amit | 700
-- 4 | Chen | 200
-- 5 | Bina | 400
-- 6 | Amit | 100
SELECT customer, COUNT(*)
FROM orders
GROUP BY customer
HAVING COUNT(*) >= 2
ORDER BY customer;- Amit 2, Bina 2
- Amit 3
- Amit 3, Bina 2
- Amit 3, Bina 2, Chen 1
Answer
C. Amit 3, Bina 2 Amit has 3 orders, Bina 2, Chen 1 (filtered out).
69. What is the output? (Note: WHERE runs before GROUP BY.)
-- orders
-- id | customer | amount
-- 1 | Amit | 500
-- 2 | Bina | 300
-- 3 | Amit | 700
-- 4 | Chen | 200
-- 5 | Bina | 400
-- 6 | Amit | 100
SELECT customer, SUM(amount)
FROM orders
WHERE amount > 200
GROUP BY customer
ORDER BY customer;- Amit 1300, Bina 700, Chen 200
- Amit 1200, Bina 700, Chen 0
- Amit 1300, Bina 700
- Amit 1200, Bina 700
Answer
D. Amit 1200, Bina 700 WHERE removes the 200 and 100 orders first. Amit: 500 + 700, Bina: 300 + 400, and Chen has no rows left.
70. What is the output?
-- orders
-- id | customer | amount
-- 1 | Amit | 500
-- 2 | Bina | 300
-- 3 | Amit | 700
-- 4 | Chen | 200
-- 5 | Bina | 400
-- 6 | Amit | 100
SELECT customer, MAX(amount)
FROM orders
GROUP BY customer
ORDER BY customer;- Amit 500, Bina 300, Chen 200
- Amit 700
- Amit 700, Bina 400, Chen 200
- Amit 1300, Bina 700, Chen 200
Answer
C. Amit 700, Bina 400, Chen 200 MAX is taken within each customer's group.
71. Which customer does this return?
-- orders
-- id | customer | amount
-- 1 | Amit | 500
-- 2 | Bina | 300
-- 3 | Amit | 700
-- 4 | Chen | 200
-- 5 | Bina | 400
-- 6 | Amit | 100
SELECT customer
FROM orders
GROUP BY customer
ORDER BY SUM(amount) DESC
LIMIT 1;- Chen
- Amit, Bina
- Amit
- Bina
Answer
C. Amit Amit has the largest total (1300).
72. What is the output?
-- orders
-- id | customer | amount
-- 1 | Amit | 500
-- 2 | Bina | 300
-- 3 | Amit | 700
-- 4 | Chen | 200
-- 5 | Bina | 400
-- 6 | Amit | 100
SELECT customer FROM orders WHERE amount > 400
UNION
SELECT customer FROM orders WHERE amount < 300
ORDER BY customer;- Chen
- Amit, Bina, Chen
- Amit, Chen
- Amit, Amit, Amit, Chen
Answer
C. Amit, Chen > 400 gives Amit twice; < 300 gives Chen and Amit. UNION removes duplicates: Amit, Chen.
73. How many rows does UNION ALL give here?
-- orders
-- id | customer | amount
-- 1 | Amit | 500
-- 2 | Bina | 300
-- 3 | Amit | 700
-- 4 | Chen | 200
-- 5 | Bina | 400
-- 6 | Amit | 100
SELECT COUNT(*) FROM (
SELECT customer FROM orders WHERE amount > 400
UNION ALL
SELECT customer FROM orders WHERE amount < 300
);- 3
- 4
- 2
- 6
Answer
B. 4 UNION ALL keeps duplicates: 2 rows (> 400) + 2 rows (< 300) = 4.
74. What does this return?
-- orders
-- id | customer | amount
-- 1 | Amit | 500
-- 2 | Bina | 300
-- 3 | Amit | 700
-- 4 | Chen | 200
-- 5 | Bina | 400
-- 6 | Amit | 100
SELECT customer FROM orders WHERE amount >= 400
INTERSECT
SELECT customer FROM orders WHERE amount <= 300
ORDER BY customer;- Amit
- Bina, Chen
- Amit, Bina, Chen
- Amit, Bina
Answer
D. Amit, Bina Customers with an order ≥ 400: Amit, Bina. With an order ≤ 300: Bina, Chen, Amit. Both: Amit, Bina.
75. What does this return?
-- orders
-- id | customer | amount
-- 1 | Amit | 500
-- 2 | Bina | 300
-- 3 | Amit | 700
-- 4 | Chen | 200
-- 5 | Bina | 400
-- 6 | Amit | 100
SELECT customer FROM orders
EXCEPT
SELECT customer FROM orders WHERE amount > 350;- No rows
- Bina
- Chen
- Amit, Chen
Answer
C. Chen Customers with an order above 350 are Amit and Bina; removing them leaves Chen.
76. Which join returns only the rows that have matching values in both tables?
- INNER JOIN
- LEFT JOIN
- FULL OUTER JOIN
- CROSS JOIN
Answer
A. INNER JOIN An inner join keeps matching rows only.
77. Which join returns all rows from the left table and the matching rows from the right table?
- INNER JOIN
- LEFT JOIN
- SELF JOIN
- RIGHT JOIN
Answer
B. LEFT JOIN Unmatched left rows get NULLs for the right table's columns.
78. In a LEFT JOIN, what appears in the right table's columns when there is no match?
- The previous row's value
- NULL
- An empty string
- 0
Answer
B. NULL Missing values are filled with NULL.
79. Joining a table to itself, for example to pair employees with their managers, is called a:
- Natural join
- Self join
- Outer join
- Cross join
Answer
B. Self join The same table is used twice with different aliases.
80. A CROSS JOIN between a table of 4 rows and a table of 6 rows returns:
- 4 rows
- 24 rows
- 10 rows
- 6 rows
Answer
B. 24 rows A cross join returns the Cartesian product: 4 × 6.
81. A NATURAL JOIN joins two tables on:
- Every possible pair of rows
- Columns chosen at random
- The primary key only
- All columns with the same name in both tables
Answer
D. All columns with the same name in both tables It matches on all same-named columns automatically.
82. What is the logical order in which these clauses are evaluated?
- FROM → GROUP BY → WHERE → SELECT → HAVING → ORDER BY
- FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY
- SELECT → WHERE → FROM → ORDER BY → GROUP BY → HAVING
- SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY
Answer
B. FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY This is why a column alias from SELECT cannot be used in WHERE.
83. Which statement about WHERE and HAVING is correct?
- They are exactly the same
- HAVING runs before WHERE
- WHERE filters rows before grouping; HAVING filters groups after grouping
- WHERE can use aggregate functions like SUM directly
Answer
C. WHERE filters rows before grouping; HAVING filters groups after grouping Aggregates such as SUM(amount) > 600 belong in HAVING.
84. What is the difference between UNION and UNION ALL?
- UNION joins columns side by side
- There is no difference
- UNION ALL removes duplicates; UNION keeps them
- UNION removes duplicate rows; UNION ALL keeps them
Answer
D. UNION removes duplicate rows; UNION ALL keeps them UNION ALL is faster because it skips duplicate removal.
85. A subquery that refers to a column of the outer query and runs once for each outer row is a:
- Common table expression
- Correlated subquery
- Scalar subquery
- Derived table
Answer
B. Correlated subquery Example: WHERE e.salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id).
86. Which query correctly finds employees whose department does not exist in the departments table?
- INNER JOIN departments, then WHERE departments.id IS NULL
- WHERE dept_id = NULL
- LEFT JOIN departments, then WHERE departments.id IS NULL
- CROSS JOIN departments
Answer
C. LEFT JOIN departments, then WHERE departments.id IS NULL An inner join already drops unmatched rows, and "= NULL" never matches.
87. Which aggregate counts every row, including rows with NULL values?
- COUNT(column)
- SUM(column)
- AVG(column)
- COUNT(*)
Answer
D. COUNT(*) COUNT(column) and the other aggregates skip NULLs.
88. Which keyword removes duplicate rows from a SELECT result?
- GROUP
- UNIQUE
- DISTINCT
- DIFFERENT
Answer
C. DISTINCT SELECT DISTINCT city FROM students;
89. Which statement is used to remove all rows from a table quickly while keeping its structure?
- TRUNCATE TABLE
- DROP TABLE
- DELETE DATABASE
- REMOVE TABLE
Answer
A. TRUNCATE TABLE DROP removes the table itself; TRUNCATE empties it.
90. Which SQL clause names a temporary result set that can be used in the main query, like a named subquery?
- INTO
- HAVING
- WITH (a common table expression)
- GROUP BY
Answer
C. WITH (a common table expression) WITH high AS (SELECT …) SELECT * FROM high;
91. In DBMS & SQL, which description best fits “primary key”?
- filter groups after aggregation
- transaction guarantees of atomicity consistency isolation durability
- keep all rows from left table with matched right rows
- uniquely identify a row
Answer
D. uniquely identify a row primary key is used to uniquely identify a row.
92. In DBMS & SQL, which description best fits “foreign key”?
- keep all rows from left table with matched right rows
- reference a key in another table
- reduce redundancy through schema design
- accelerate selected lookup operations
Answer
B. reference a key in another table foreign key is used to reference a key in another table.
93. In DBMS & SQL, which description best fits “INNER JOIN”?
- uniquely identify a row
- return matching rows from both tables
- reduce redundancy through schema design
- reference a key in another table
Answer
B. return matching rows from both tables INNER JOIN is used to return matching rows from both tables.
94. In DBMS & SQL, which description best fits “LEFT JOIN”?
- filter groups after aggregation
- keep all rows from left table with matched right rows
- return matching rows from both tables
- reduce redundancy through schema design
Answer
B. keep all rows from left table with matched right rows LEFT JOIN is used to keep all rows from left table with matched right rows.
95. In DBMS & SQL, which description best fits “GROUP BY”?
- filter groups after aggregation
- keep all rows from left table with matched right rows
- form groups for aggregation
- reference a key in another table
Answer
C. form groups for aggregation GROUP BY is used to form groups for aggregation.
96. In DBMS & SQL, which description best fits “HAVING”?
- filter groups after aggregation
- keep all rows from left table with matched right rows
- reduce redundancy through schema design
- group operations into a logical unit
Answer
A. filter groups after aggregation HAVING is used to filter groups after aggregation.
97. In DBMS & SQL, which description best fits “index”?
- form groups for aggregation
- reference a key in another table
- accelerate selected lookup operations
- reduce redundancy through schema design
Answer
C. accelerate selected lookup operations index is used to accelerate selected lookup operations.
98. In DBMS & SQL, which description best fits “transaction”?
- reference a key in another table
- accelerate selected lookup operations
- form groups for aggregation
- group operations into a logical unit
Answer
D. group operations into a logical unit transaction is used to group operations into a logical unit.
99. In DBMS & SQL, which description best fits “normalization”?
- return matching rows from both tables
- keep all rows from left table with matched right rows
- reduce redundancy through schema design
- group operations into a logical unit
Answer
C. reduce redundancy through schema design normalization is used to reduce redundancy through schema design.
100. In DBMS & SQL, which description best fits “ACID”?
- group operations into a logical unit
- transaction guarantees of atomicity consistency isolation durability
- reduce redundancy through schema design
- keep all rows from left table with matched right rows
Answer
B. transaction guarantees of atomicity consistency isolation durability ACID is used to transaction guarantees of atomicity consistency isolation durability.
101. Which DBMS & SQL concept matches this description: “uniquely identify a row”?
- foreign key
- HAVING
- primary key
- ACID
Answer
C. primary key The definition describes primary key.
102. Which DBMS & SQL concept matches this description: “reference a key in another table”?
- INNER JOIN
- GROUP BY
- foreign key
- LEFT JOIN
Answer
C. foreign key The definition describes foreign key.
103. Which DBMS & SQL concept matches this description: “return matching rows from both tables”?
- primary key
- INNER JOIN
- GROUP BY
- ACID
Answer
B. INNER JOIN The definition describes INNER JOIN.
104. Which DBMS & SQL concept matches this description: “keep all rows from left table with matched right rows”?
- LEFT JOIN
- normalization
- foreign key
- primary key
Answer
A. LEFT JOIN The definition describes LEFT JOIN.
105. Which DBMS & SQL concept matches this description: “form groups for aggregation”?
- normalization
- foreign key
- GROUP BY
- index
Answer
C. GROUP BY The definition describes GROUP BY.
106. Which DBMS & SQL concept matches this description: “filter groups after aggregation”?
- HAVING
- primary key
- GROUP BY
- LEFT JOIN
Answer
A. HAVING The definition describes HAVING.
107. Which DBMS & SQL concept matches this description: “accelerate selected lookup operations”?
- ACID
- index
- transaction
- LEFT JOIN
Answer
B. index The definition describes index.
108. Which DBMS & SQL concept matches this description: “group operations into a logical unit”?
- transaction
- foreign key
- ACID
- INNER JOIN
Answer
A. transaction The definition describes transaction.
109. Which DBMS & SQL concept matches this description: “reduce redundancy through schema design”?
- transaction
- GROUP BY
- LEFT JOIN
- normalization
Answer
D. normalization The definition describes normalization.
110. Which DBMS & SQL concept matches this description: “transaction guarantees of atomicity consistency isolation durability”?
- primary key
- normalization
- transaction
- ACID
Answer
D. ACID The definition describes ACID.
111. A table has 15 rows. An INSERT adds 5 rows and no rows are deleted. How many rows remain?
- 0
- 50
- 20
- 19
Answer
C. 20 The new row count is 15+5=20.
Mock test
- DBMS and SQL Mock Test 15 questions · 15 min