0% found this document useful (0 votes)
12 views16 pages

SQL All in One IQ

SQL (Structured Query Language) is a standard programming language for managing relational databases, enabling CRUD operations and database structure definitions. It includes various command types such as DDL, DML, DCL, TCL, and DQL, and concepts like primary and foreign keys, constraints, and normalization. Additionally, SQL supports complex operations like joins, subqueries, and transactions, while ensuring data integrity and security through features like stored procedures and role-based access control.

Uploaded by

Gaurav Dhakate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

SQL All in One IQ

SQL (Structured Query Language) is a standard programming language for managing relational databases, enabling CRUD operations and database structure definitions. It includes various command types such as DDL, DML, DCL, TCL, and DQL, and concepts like primary and foreign keys, constraints, and normalization. Additionally, SQL supports complex operations like joins, subqueries, and transactions, while ensuring data integrity and security through features like stored procedures and role-based access control.

Uploaded by

Gaurav Dhakate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SQL

1. What is SQL?
SQL (Structured Query Language) is a standard programming language used to manage and manipulate
relational databases. It allows users to create, read, update, and delete data (CRUD operations) and define
database structures. SQL is essential for interacting with databases like MySQL, PostgreSQL, Oracle, and SQL
Server.
2. What are the different types of SQL commands?
SQL commands can be categorized into five main types:
1. DDL (Data Definition Language) – Commands like CREATE, ALTER, DROP, and TRUNCATE to define or
modify database structures.
2. DML (Data Manipulation Language) – Commands like SELECT, INSERT, UPDATE, and DELETE to
manipulate data.
3. DCL (Data Control Language) – Commands like GRANT and REVOKE to manage user permissions.
4. TCL (Transaction Control Language) – Commands like COMMIT, ROLLBACK, and SAVEPOINT to handle
transactions.
5. DQL (Data Query Language) – Primarily the SELECT statement to query data.
3. Explain the difference between DELETE and TRUNCATE.
• DELETE is a DML command that removes specific rows from a table based on a WHERE clause. It logs
each row deletion, can be rolled back, and fires triggers.
• TRUNCATE is a DDL command that removes all rows from a table without logging individual row
deletions. It resets the table, is faster, and cannot be rolled back (in most databases).
4. What is a primary key?
A primary key is a column (or set of columns) that uniquely identifies each row in a table. It enforces
uniqueness and cannot contain NULL values. A table can have only one primary key, often used for indexing
and relationships.
5. What is a foreign key?
A foreign key is a column that establishes a relationship between two tables by referencing the primary key of
another table. It ensures referential integrity, meaning it restricts actions that would destroy links between
tables.
6. What are constraints in SQL?
Constraints are rules applied to columns to enforce data integrity. Common constraints include:
• PRIMARY KEY (uniquely identifies rows)
• FOREIGN KEY (ensures referential integrity)
• NOT NULL (disallows NULL values)
• UNIQUE (ensures all values are distinct)
• CHECK (validates data against a condition)
• DEFAULT (sets a default value if none is provided)
7. What is the difference between CHAR and VARCHAR data types?
• CHAR is fixed-length and pads spaces if the data is shorter than the defined length
(e.g., CHAR(10) always uses 10 bytes).
• VARCHAR is variable-length and uses only the space needed (e.g., VARCHAR(10) stores up to 10
characters but only consumes actual length + 1-2 bytes for overhead).
8. Explain the use of the SELECT statement.
The SELECT statement retrieves data from one or more tables. It can include:
• Column selection: SELECT column1, column2
• Filtering: WHERE clause
• Sorting: ORDER BY
• Aggregation: GROUP BY and aggregate functions
Example:
SELECT name, salary FROM employees WHERE department = 'IT' ORDER BY salary DESC;
9. What is the purpose of the WHERE clause?
The WHERE clause filters records based on specified conditions, returning only rows that meet the criteria.
Example:
SELECT * FROM customers WHERE age > 30;
10. How do you sort records in SQL?
Use the ORDER BY clause to sort results in ascending (ASC) or descending (DESC) order. Example:
SELECT * FROM products ORDER BY price DESC;
11. What is the difference between UNION and UNION ALL?
• UNION combines results from two queries, removes duplicates, and sorts the output.
• UNION ALL combines results but retains duplicates and does not sort, making it faster.
12. Explain the use of the LIKE operator.
The LIKE operator searches for patterns in text using wildcards:
• % matches any sequence of characters.
• _ matches a single character.
Example:
SELECT * FROM employees WHERE name LIKE 'A%'; -- Names starting with 'A'
13. What are aggregate functions in SQL?
Aggregate functions perform calculations on a set of values and return a single value. Common ones:
• COUNT() – Number of rows
• SUM() – Total of values
• AVG() – Average
• MAX()/MIN() – Highest/lowest value
Example:
SELECT AVG(salary) FROM employees;
14. What is the difference between HAVING and WHERE clauses?
• WHERE filters rows before aggregation.
• HAVING filters groups after aggregation (used with GROUP BY).
Example:
SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000;
15. What is a subquery?
A subquery (nested query) is a query inside another query, used to return data for the main query. Example:
SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
16. Explain the concept of NULL values in SQL.
NULL represents missing or unknown data. It is not zero or an empty string. Use IS NULL or IS NOT NULL to
check for NULL values.
17. What is the BETWEEN operator?
BETWEEN filters values within a range (inclusive). Example:
SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';
18. How do you remove duplicate records from a table?
Use DISTINCT in a query or GROUP BY to eliminate duplicates. For deleting duplicates from a table:
DELETE FROM table_name WHERE id NOT IN (SELECT MIN(id) FROM table_name GROUP BY column1,
column2);
19. What is the difference between INNER JOIN and OUTER JOIN?
• INNER JOIN returns only matching rows from both tables.
• OUTER JOIN returns all rows from one table and matching rows from the other (LEFT, RIGHT, or FULL).
20. What is a view in SQL?
A view is a virtual table based on the result of a query. It simplifies complex queries, enhances security, and
does not store data physically. Example:
CREATE VIEW active_customers AS SELECT * FROM customers WHERE status = 'Active';
21. Explain the use of the GROUP BY clause.
GROUP BY groups rows with the same values into summary rows, often used with aggregate functions.
Example:
SELECT department, COUNT(*) FROM employees GROUP BY department;
22. What is the difference between DROP, DELETE, and TRUNCATE?
• DELETE removes rows (can be rolled back).
• TRUNCATE removes all rows quickly (DDL, resets the table).
• DROP deletes the entire table structure (irreversible)
23. What is normalization?
Normalization is the process of organizing data to minimize redundancy by dividing tables and defining
relationships. It improves data integrity and efficiency.
24. What are the different normal forms?
1. 1NF – Atomic values, no repeating groups.
2. 2NF – Meets 1NF + no partial dependency (all non-key columns depend on the entire primary key).
3. 3NF – Meets 2NF + no transitive dependency (non-key columns depend only on the primary key).
Higher forms include BCNF, 4NF, and 5NF.
25. What is denormalization?
Denormalization intentionally introduces redundancy to improve read performance, often used in data
warehouses where query speed is prioritized over update efficiency.
26. What are indexes in SQL?
Indexes are database objects that improve query performance by speeding up data retrieval. They work like a
book’s index, allowing the database engine to locate rows quickly without scanning the entire table. Common
types include clustered and non-clustered indexes.
Example:
CREATE INDEX idx_customer_name ON customers(name);
27. Explain the difference between clustered and non-clustered indexes.
• Clustered Index determines the physical order of data in a table (only one per table). Example: Primary
key.
• Non-Clustered Index is a separate structure that stores a reference to the actual data (multiple
allowed).
Key Difference:
• Clustered = Data is stored in index order.
• Non-Clustered = Index points to data location.
28. What is a self-join?
A self-join is a join where a table is joined with itself, typically to compare rows within the same table.
Example (Employees and their Managers):
SELECT e1.name AS employee, e2.name AS manager
FROM employees e1
JOIN employees e2 ON e1.manager_id = e2.id;
29. What is a cross join?
A cross join (Cartesian product) returns all possible combinations of rows from two tables. It has no join
condition.
Example:
SELECT * FROM table1 CROSS JOIN table2;
30. How do you use the CASE statement in SQL?
The CASE statement provides conditional logic in SQL, similar to if-else.
Example (Categorizing salaries):
SELECT name,
CASE
WHEN salary > 100000 THEN 'High'
WHEN salary > 50000 THEN 'Medium'
ELSE 'Low'
END AS salary_category
FROM employees;
31. What are stored procedures?
Stored procedures are precompiled SQL statements stored in the database for reuse. They improve
performance, security, and modularity.
Example (Creating a simple SP):
CREATE PROCEDURE GetEmployeesByDept(IN dept VARCHAR(50))
AS
BEGIN
SELECT * FROM employees WHERE department = dept;
END;
32. What are triggers in SQL?
Triggers are automatic procedures that execute in response to specific events (e.g., INSERT, UPDATE, DELETE)
on a table.
Example (Audit Log Trigger):
CREATE TRIGGER log_changes
AFTER UPDATE ON employees
FOR EACH ROW
INSERT INTO audit_log VALUES (OLD.salary, NEW.salary, NOW());
33. Explain the ACID properties in a database.
Answer:
ACID ensures reliable transactions:
• Atomicity – Transactions are all-or-nothing.
• Consistency – Data remains valid before/after transactions.
• Isolation – Concurrent transactions don’t interfere.
• Durability – Committed transactions persist even after failures.
34. What is a transaction in SQL?
A transaction is a sequence of SQL operations executed as a single logical unit. It ensures data integrity
using COMMIT (save changes) or ROLLBACK (undo changes).
Example:
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT; -- Or ROLLBACK if an error occurs
35. How do you implement error handling in SQL?
Use TRY...CATCH (SQL Server) or EXCEPTION blocks (Oracle/PostgreSQL).
Example (SQL Server):
BEGIN TRY
INSERT INTO employees VALUES (NULL); -- Invalid
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS error;
END CATCH;
36. What is a cursor?
A cursor is a database object used to retrieve and manipulate rows one at a time (row-by-row processing).
Example:
DECLARE employee_cursor CURSOR FOR
SELECT name FROM employees;
OPEN employee_cursor;
FETCH NEXT FROM employee_cursor;
CLOSE employee_cursor;
37. What is the difference between VARCHAR and TEXT?
• VARCHAR(n) has a defined length limit (e.g., VARCHAR(255)).
• TEXT stores large, variable-length strings (no explicit limit).
Use Case:
• VARCHAR for short strings (names, emails).
• TEXT for long content (articles, logs).
38. How do you optimize a SQL query?
• Use indexes on frequently queried columns.
• Avoid `SELECT * – Fetch only needed columns.
• Limit joins and use proper join types.
• Analyze query execution plans to identify bottlenecks.
39. What is a Common Table Expression (CTE)?
A CTE is a temporary result set defined within a query (improves readability and recursion).
Example:
WITH high_salary_employees AS (
SELECT * FROM employees WHERE salary > 100000
)
SELECT * FROM high_salary_employees;
40. Explain RANK(), DENSE_RANK(), and ROW_NUMBER().
• ROW_NUMBER() – Unique sequential numbers (no ties).
• RANK() – Skips ranks after ties (1, 2, 2, 4).
• DENSE_RANK() – No rank gaps (1, 2, 2, 3).
Example:
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;
41. What are window functions?
Window functions perform calculations across a set of rows related to the current row, without collapsing
rows.
Example (Running Total):
SELECT date, revenue,
SUM(revenue) OVER (ORDER BY date) AS running_total
FROM sales;
42. How do you handle NULL values in calculations?
Use COALESCE() or ISNULL() to replace NULL with a default value.
Example:
SELECT COALESCE(salary, 0) FROM employees; -- Replaces NULL with 0
43. Difference between COALESCE() and ISNULL()?
Answer:
• COALESCE() (ANSI standard) accepts multiple arguments and returns the first non-NULL value.
• ISNULL() (SQL Server-specific) takes two arguments and replaces NULL with the second.
Example:
SELECT COALESCE(NULL, NULL, 'default'); -- Returns 'default'
SELECT ISNULL(NULL, 'default'); -- Returns 'default'
44. What is the MERGE statement?
Answer:
MERGE (UPSERT) combines INSERT, UPDATE, and DELETE in a single operation based on a condition.
Example:
MERGE INTO target_table t
USING source_table s ON t.id = s.id
WHEN MATCHED THEN UPDATE SET t.value = s.value
WHEN NOT MATCHED THEN INSERT (id, value) VALUES (s.id, s.value);
45. How do you retrieve the current date and time?
• SQL Server: SELECT GETDATE();
• MySQL: SELECT NOW();
• PostgreSQL: SELECT CURRENT_TIMESTAMP;
46. Difference between DATETIME and TIMESTAMP?
• DATETIME stores date and time (no timezone, larger storage).
• TIMESTAMP stores Unix time (timezone-aware, smaller storage, auto-updates in some DBs).
47. How do you calculate date differences?
• SQL Server: DATEDIFF(day, date1, date2)
• MySQL: DATEDIFF(date1, date2)
• PostgreSQL: date2 - date1
48. What is the IF EXISTS clause?
IF EXISTS checks if an object (table, column, etc.) exists before performing an operation.
Example (Drop table if it exists):
DROP TABLE IF EXISTS temp_table;
49. How do you update data from another table?
Use a JOIN in the UPDATE statement.
Example:
UPDATE employees e
SET e.salary = s.new_salary
FROM salary_updates s
WHERE e.id = s.employee_id;
50. Difference between EXISTS and IN?
• EXISTS stops processing after the first match (faster for subqueries).
• IN checks all values in a list (better for static lists).
Example:
SELECT * FROM orders o
WHERE EXISTS (SELECT 1 FROM customers c WHERE o.customer_id = c.id);
51. What are Materialized Views?
Materialized views store the result of a query physically in the database. They're faster for repeated access and
support periodic refreshes, unlike regular views which fetch data in real-time.
52. Explain Database Partitioning.
Partitioning splits a large table into smaller parts for better performance and easier management. It's done
based on range, list, hash, etc., but the table behaves as one logically.
53. What is Sharding in Databases?
Sharding distributes data across multiple servers. Each shard holds part of the data, helping applications scale
horizontally and handle large loads efficiently.
54. How do you implement Recursive Queries in SQL?
I use WITH RECURSIVE CTEs to fetch hierarchical or tree-structured data. The base query selects initial rows,
and the recursive part repeatedly joins to get child rows.
55. Difference between OLTP and OLAP?
OLTP handles real-time transactions (insert/update), while OLAP is used for analysis and reporting on large
volumes of historical data. OLTP is write-heavy; OLAP is read-heavy.

Feature OLTP (Online Transaction Processing) OLAP (Online Analytical Processing)


Purpose Handles daily transactions Supports complex analytics
Operations Insert, Update, Delete Select (Read-heavy)
Schema Highly normalized Denormalized
Users End-users, clerks Analysts, business users
Speed Fast for write operations Fast for read and reporting

56. What is Data Warehousing?


It’s a centralized system that stores data from different sources for analysis and reporting. It's optimized for
querying, not transactions.
57. What is a Star Schema?
A star schema has a central fact table linked to dimension tables. It’s simple and fast for queries used in
reporting and analytics.
58. What is a Snowflake Schema?
It’s a normalized version of the star schema where dimension tables are further split to reduce redundancy,
though it involves more joins.
59. How do you perform ETL in SQL?
I extract data using SELECT, transform it with SQL functions (joins, filters), and load it using INSERT, UPDATE, or
MERGE into target tables.
60. Best Practices for Database Security?
Use least privilege, encrypt data, prevent SQL injection with parameterized queries, audit activity, and keep
systems patched and backed up.
61. How do you implement Role-Based Access Control?
Define roles with specific permissions, assign users to roles. It simplifies access management and ensures
users have only what they need.
62. Symmetric vs Asymmetric Encryption?
Symmetric uses one key for both encryption and decryption. Asymmetric uses a public/private key pair —
more secure but slower.
63. Ensuring Data Integrity in Distributed DBs?
Use two-phase commit, unique IDs, conflict resolution, and replication monitoring to keep data consistent
across distributed nodes.
64. How do you audit changes in a database (INSERT/UPDATE/DELETE)?
You can audit changes using triggers, which log changes into an audit table. Some databases also offer in-built
auditing like Oracle’s AUDIT command or SQL Server’s Change Data Capture (CDC) and SQL Server Audit.
65. What is a deadlock? How do you resolve it?
A deadlock occurs when two or more transactions wait on resources locked by each other, preventing
completion. It's resolved by the DBMS automatically choosing one transaction as a victim and rolling it back.
To avoid it, always access tables in the same order and use shorter transactions.
66. How do you implement row-level security in SQL?
Row-level security is implemented using policies or views that restrict row access based on user roles. SQL
Server and PostgreSQL support RLS using CREATE POLICY, while others may use views with WHERE conditions
tied to session variables.
67. What is query execution plan and how to read it?
A query execution plan shows how the database will execute a SQL query, including indexes used, join types,
and row estimates. In SQL Server, use SET SHOWPLAN_ALL or “Display Estimated Execution Plan.” Look for
expensive operations like table scans and optimize accordingly.
68. How do you use WITH TIES in SQL Server?
WITH TIES is used with TOP to include extra rows that have the same value as the last row based on the
ORDER BY clause.
Example:
SELECT TOP 3 WITH TIES * FROM Products ORDER BY Price DESC;
69. What are surrogate keys vs natural keys?
• Natural Key: Real-world data like email or SSN.
• Surrogate Key: Artificial identifier like an auto-increment ID.
Surrogate keys are preferred for stability and simplicity.
70. How do you detect and remove duplicate records from a table?
Use GROUP BY with HAVING COUNT(*) > 1 to detect duplicates.
To remove:
DELETE FROM Employees
WHERE ID NOT IN (SELECT MIN(ID) FROM Employees GROUP BY Name, Email);
71. How do you implement pagination in SQL queries?
Use OFFSET and FETCH in SQL Server/MySQL/PostgreSQL:
SELECT * FROM Products ORDER BY ProductID
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
72. What is lateral join or cross apply?
CROSS APPLY (SQL Server) or LATERAL (PostgreSQL) allows a subquery or function to reference columns from
the outer query. Useful when the inner query depends on the outer row.
73. What is the purpose of INTERSECT and EXCEPT in SQL?
• INTERSECT: Returns common rows from two queries.
• EXCEPT: Returns rows from the first query that are not in the second.
Both eliminate duplicates by default.
74. What are the differences between DELETE with JOIN vs EXISTS?
• JOIN may delete more rows if there are multiple matches.
• EXISTS is safer and faster when checking for existence only, avoiding duplicates.
75. How do you monitor and improve database performance?
Use tools like SQL Profiler, EXPLAIN PLAN, or Performance Monitor. Improve performance with indexes, query
optimization, normalization, and caching strategies.
76. What is a temporary table vs global temp table?
• Temporary Table (#Temp): Visible only in the current session.
• Global Temp Table (##Temp): Visible to all sessions until closed.
77. How do you create dynamic SQL queries?
By building SQL statements as strings and executing with EXEC or sp_executesql (SQL Server).
DECLARE @sql NVARCHAR(MAX) = 'SELECT * FROM ' + @tableName;
EXEC sp_executesql @sql;
78. How do you implement soft delete in SQL?
Add a column like is_deleted or deleted_at. Instead of DELETE, update this flag. Queries should filter WHERE
is_deleted = 0.
79. Explain isolation levels in transactions.
• Read Uncommitted – Dirty reads allowed
• Read Committed – No dirty reads
• Repeatable Read – No dirty/phantom reads
• Serializable – Full isolation, prevents phantom rows
Each level increases data consistency but may reduce concurrency.
80. What is the difference between Serializable and Snapshot isolation levels?
• Serializable locks rows and prevents concurrent inserts/updates.
• Snapshot uses versioning without locking, improving concurrency.
Snapshot reads a consistent snapshot at the start of the transaction.
81. What are indexed views? When should you use them?
Indexed views are materialized views with physical storage. They're useful when querying aggregated or
joined data repeatedly. Use when performance is critical and data updates are relatively infrequent.
82. What is query hint and when is it used?
Query hints override the query optimizer decisions.
Example: OPTION (HASH JOIN) or FORCESEEK.
Use when the optimizer chooses a suboptimal plan.
83. How do you migrate a large dataset between tables?
Use batch inserts, INSERT INTO ... SELECT, or tools like bcp, SQL Loader, or ETL tools. Consider disabling
constraints or indexes during bulk load.
84. How does SQL handle concurrency control?
Through locks and isolation levels.
• Shared locks for reads
• Exclusive locks for writes
• Deadlock detection and transaction management ensure consistency
85. What is the difference between compile-time and run-time errors in SQL procedures?
• Compile-time: Syntax issues, incorrect object names, etc.
• Run-time: Division by zero, constraint violations, or missing data during execution.
86. How can you prevent SQL injection?
Use parameterized queries, stored procedures, and input validation. Avoid dynamic SQL with unsanitized user
inputs.
87. What are recursive CTEs and give an example use case?
A recursive CTE calls itself to process hierarchical or tree data.
Example: Employee-manager hierarchy.
WITH EmployeeCTE AS (
SELECT ID, Name, ManagerID FROM Employees WHERE ManagerID IS NULL
UNION ALL
SELECT e.ID, e.Name, e.ManagerID FROM Employees e
JOIN EmployeeCTE c ON e.ManagerID = c.ID
)
SELECT * FROM EmployeeCTE;
88. What is the use of DECODE() and CASE in Oracle/SQL Server?
Both are used for conditional logic in SQL.
• DECODE is Oracle-specific.
• CASE is ANSI standard and more powerful.
SELECT Name,
CASE
WHEN Salary > 50000 THEN 'High'
ELSE 'Low'
END AS SalaryGroup
FROM Employees;
89. Explain database normalization vs indexing.
• Normalization: Reduces redundancy by organizing tables into 1NF, 2NF, etc.
• Indexing: Improves read performance by creating pointers to data.
Both serve different purposes but work together for optimal design.
90. What’s the use of PIVOT and UNPIVOT operations in SQL?
• PIVOT converts rows into columns, useful for summary reports.
• UNPIVOT converts columns into rows.
Used in reporting and data transformation.
Basic Practice Questions (1–15)
1. Create a table Students with columns id, name, age, marks.
2. Insert 5 records into the Students table.
3. Write a query to retrieve all student names.
4. Retrieve students with marks greater than 60.
5. Update a student’s name based on id.
6. Delete a student where id = 2.
7. Add a new column email to the Students table.
8. Rename the column marks to score.
9. Write a query to find students between age 18 and 22.
10. Use ORDER BY to sort students by marks descending.
11. Find total number of students using COUNT().
12. Find the average marks using AVG().
13. Find highest and lowest marks.
14. Group students by age and get average score.
15. Use LIKE to find student names starting with 'A'.

Intermediate Practice (16–30)


16. Create two tables: Departments(dept_id, dept_name) and Employees(emp_id, name, salary, dept_id).
17. Insert records into both tables.
18. Retrieve employee names along with their department name using JOIN.
19. Get the number of employees in each department.
20. Find departments having more than 2 employees (HAVING).
21. Retrieve employees with the second-highest salary.
22. Find the employee(s) with the maximum salary in each department.
23. Write a query using CASE to categorize employees as 'Low', 'Medium', or 'High' earners.
24. Delete employees who are not assigned to any department (dept_id IS NULL).
25. Retrieve employees who belong to either 'HR' or 'IT' departments.
26. Use IN and NOT IN with subqueries for filtering.
27. Write a query using EXISTS to find departments having employees.
28. Get the department with no employees (LEFT JOIN + WHERE NULL).
29. Write a nested subquery to find the highest-paid employee.
30. Rename the Employees table to Staff.

Advanced Practice (31–45)


31. Create a trigger to log INSERT into a log table.
32. Implement a view that shows employee name, salary, and department.
33. Write a CTE to show employee-manager hierarchy.
34. Use a recursive CTE to flatten a tree structure.
35. Create an indexed column and show performance difference.
36. Create a UNION query combining employees and interns.
37. Write a query using INTERSECT to find common employees between two departments.
38. Use PIVOT to transform monthly sales from rows to columns.
39. Use UNPIVOT to transform sales columns back to rows.
40. Write a dynamic SQL to select from a table whose name is passed as a parameter.
41. Create a stored procedure that accepts salary and returns employee names.
42. Create a function to calculate yearly bonus based on salary.
43. Implement soft delete using an is_deleted flag.
44. Write a query with OFFSET and FETCH for pagination.
45. Create a composite primary key in a table.

Hands-On Tasks (46–50)


46. Export data from a table to a .csv file.
47. Import .csv data into a SQL table.
48. Create a backup of a database and restore it.
49. Monitor query execution time and tune a slow query.
50. Write a script to remove duplicate rows from a table using ROW_NUMBER().

SQL Mock Interview – Junior Developer Role


1. Hi! Let's start with your experience. Can you tell me about your familiarity with SQL and how you've
used it in projects?
2. Can you explain the difference between WHERE and HAVING clauses with an example?
3. What types of joins have you worked with, and when would you use a LEFT JOIN instead of an INNER
JOIN?
4. Say a query is running very slowly — how would you go about optimizing it?
5. What is an index in SQL? Can it ever slow down performance?
6. Can you explain the difference between DELETE, TRUNCATE, and DROP?
7. Let’s say you have to handle NULL values in a report. How would you treat them?
8. What are SQL constraints, and how do they help in application development?
9. How do you ensure data consistency in SQL-based applications?
10. Have you used stored procedures or functions? When would you prefer them over writing queries in
application code?
11. What’s the difference between a primary key and a unique key? Can a table have both?
12. How do foreign keys work in maintaining referential integrity?
13. Can you explain the difference between UNION and UNION ALL?
14. What are views in SQL? How are they different from materialized views?
15. Have you used Common Table Expressions (CTEs)? How are they different from subqueries?
16. Can you explain normalization and why it’s important? Just give an overview of 1NF, 2NF, and 3NF.
17. What is a transaction in SQL? What are the ACID properties?
18. Can you write a SQL query to get the second highest salary from a table?
19. What’s the purpose of the GROUP BY clause? Can you use it without aggregation?
20. How do you update data in a table based on values from another table?
21. What are triggers in SQL? Have you used them in any project?
22. Can you explain the difference between scalar functions and aggregate functions in SQL?
23. What happens if we try to insert a duplicate value in a column with a UNIQUE constraint?
24. What’s the difference between a clustered and a non-clustered index?
25. What are materialized views, and how are they useful in real-time reporting systems?
26. Can you explain what database partitioning is and when you would use it?
27. What is sharding, and how is it different from partitioning?
28. How would you write a recursive query in SQL? Can you give a use case?
29. What’s the difference between OLTP and OLAP systems? Which one is more suitable for real-time
applications?
30. Explain the concept of data warehousing. How does it help in business intelligence?
31. What is a star schema, and how is it different from a snowflake schema?
32. How do you perform ETL operations using SQL? Can you walk through the typical steps?
33. What are some best practices you follow for database security?
34. How would you implement role-based access control (RBAC) in SQL?
35. What’s the difference between symmetric and asymmetric encryption? Where are they used in
database systems?
36. How do you ensure data integrity in a distributed database setup.

Answer
1. Can you tell me about your familiarity with SQL and how you've used it in projects?
Yes, I’m quite comfortable with SQL. I’ve used it in academic and personal projects to interact with relational
databases — writing queries for CRUD operations, joining multiple tables, filtering data, and creating reports.
I’ve also practiced writing complex queries involving joins, subqueries, and aggregate functions to analyze and
extract insights from datasets.
2. Can you explain the difference between WHERE and HAVING clauses with an example?
Yes. WHERE filters rows before grouping, while HAVING filters groups after GROUP BY.
Example:
SELECT department, COUNT(*)
FROM employees
WHERE salary > 30000
GROUP BY department
HAVING COUNT(*) > 5;
Here, WHERE filters high-salary employees, and HAVING filters departments having more than 5 such
employees.
3. What types of joins have you worked with, and when would you use a LEFT JOIN instead of an INNER
JOIN?
I’ve worked with INNER JOIN, LEFT JOIN, and RIGHT JOIN.
I use INNER JOIN when I need only matching records from both tables. I use LEFT JOIN when I want all records
from the left table even if there’s no match in the right table — useful when showing unmatched or optional
data.
4. Say a query is running very slowly — how would you go about optimizing it?
I would first check the query plan and indexes. Then:
• Rewrite complex subqueries using JOINs or CTEs
• Avoid SELECT *
• Use WHERE filters effectively
• Create indexes on frequently filtered or joined columns
• Analyze data volume and try partitioning if needed
5. What is an index in SQL? Can it ever slow down performance?
An index is like a shortcut to quickly find rows in a table — it improves read/query speed. However, it can slow
down performance when inserting or updating data, because the index needs to be updated too. So, I use
indexes only where needed.
6. Can you explain the difference between DELETE, TRUNCATE, and DROP?
Yes:
• DELETE removes specific rows with a WHERE clause, logs each row, and can be rolled back.
• TRUNCATE removes all rows quickly without logging individual deletes. It can’t be rolled back in most
databases.
• DROP deletes the entire table structure from the database.
7. Let’s say you have to handle NULL values in a report. How would you treat them?
I use functions like IS NULL, COALESCE(), or IFNULL() to replace NULLs with default values. For example, if
salary is NULL, I can show 0 or "Not Available" in the report.
8. What are SQL constraints, and how do they help in application development?
SQL constraints ensure data integrity at the database level. For example, PRIMARY KEY ensures uniqueness,
FOREIGN KEY maintains relationships, CHECK enforces conditions, and NOT NULL ensures required fields. They
help catch data issues early and reduce application-side validations.
9. How do you ensure data consistency in SQL-based applications?
I use constraints, transactions with BEGIN, COMMIT, ROLLBACK, and design the schema properly with keys
and relationships. Also, I avoid dirty reads using the right isolation levels when needed.
10. Have you used stored procedures or functions? When would you prefer them over writing queries in
application code?
Yes, I’ve written basic stored procedures and functions. I prefer them when I need reusable logic close to the
data, for example, when performance is critical or when business rules should be enforced inside the
database. They also help reduce code duplication.
11. What’s the difference between a primary key and a unique key? Can a table have both?
A primary key uniquely identifies each row and doesn’t allow NULLs. A unique key also ensures uniqueness but
allows one NULL. Yes, a table can have only one primary key but multiple unique keys.
12. How do foreign keys work in maintaining referential integrity?
A foreign key creates a link between two tables. It ensures that a value in one table must exist in the
referenced column of another table — preventing invalid or orphan records, thus maintaining data
consistency.
13. Can you explain the difference between UNION and UNION ALL?
UNION combines the results of two queries and removes duplicates. UNION ALL also combines results but
keeps duplicates. UNION ALL is faster because it skips duplicate checking.
14. What are views in SQL? How are they different from materialized views?
A view is a virtual table based on a query — it doesn’t store data. A materialized view stores the query result
physically and can be refreshed. Materialized views improve performance for complex queries.
15. Have you used Common Table Expressions (CTEs)? How are they different from subqueries?
Yes, I’ve used CTEs for better readability in complex queries. Unlike subqueries, CTEs can be referenced
multiple times in the same query, and they’re easier to manage, especially for recursive logic.
16. Can you explain normalization and why it’s important? Just give an overview of 1NF, 2NF, and 3NF.
Normalization reduces redundancy and ensures data integrity.
• 1NF: No repeating groups, atomic values.
• 2NF: No partial dependencies (non-key columns depend on full primary key).
• 3NF: No transitive dependencies (non-key columns don’t depend on other non-key columns).
17. What is a transaction in SQL? What are the ACID properties?
A transaction is a set of SQL operations executed as a single unit. ACID stands for:
• Atomicity: All or none of the changes apply.
• Consistency: Maintains valid state.
• Isolation: Transactions don’t interfere.
• Durability: Changes persist after commit.
18. Can you write a SQL query to get the second highest salary from a table?
Yes, using this query:
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
19. What’s the purpose of the GROUP BY clause? Can you use it without aggregation?
GROUP BY groups rows sharing the same values so we can apply aggregate functions like COUNT or SUM.
Without aggregation, it’s not meaningful and usually throws an error unless used with constants.
20. How do you update data in a table based on values from another table?
We can use an UPDATE...JOIN query.
Example in MySQL:
UPDATE employees e
JOIN bonuses b ON e.id = b.emp_id
SET e.salary = e.salary + b.amount;
21. What are triggers in SQL? Have you used them in any project?
Triggers are predefined actions that automatically run when certain events happen on a table, like insert or
update. Yes, I’ve used triggers to log data changes and enforce rules without relying on application-side
checks.
22. Can you explain the difference between scalar functions and aggregate functions in SQL?
Scalar functions return a single value for each row — like LEN() or UPPER(). Aggregate functions work on a
group of rows and return one result — like SUM() or COUNT().
23. What happens if we try to insert a duplicate value in a column with a UNIQUE constraint?
The database will throw an error and stop the insert, because the UNIQUE constraint ensures no two rows can
have the same value in that column.
24. What’s the difference between a clustered and a non-clustered index?
A clustered index sorts and stores the data rows physically in order. You can have only one. A non-clustered
index stores a separate structure with pointers to the data and you can have many of them.
25. What are materialized views, and how are they useful in real-time reporting systems?
Materialized views are precomputed query results stored like a table. They help in real-time reporting by
improving performance — especially for complex joins and aggregations — since the data is already
processed.
26. Can you explain what database partitioning is and when you would use it?
Partitioning splits a large table into smaller segments based on a rule, like date or range. It improves
performance and manageability, especially when dealing with large datasets in reporting or archiving.
27. What is sharding, and how is it different from partitioning?
Sharding is like partitioning, but across multiple machines or databases. It’s more about horizontal scaling.
Partitioning usually stays within one database. Sharding helps when you're working with massive data
volumes.
28. How would you write a recursive query in SQL? Can you give a use case?
I’d use a Common Table Expression (CTE). For example, to get an employee-manager hierarchy:
WITH RECURSIVE emp_cte AS (
SELECT id, name, manager_id FROM employee WHERE manager_id IS NULL
UNION ALL
SELECT e.id, e.name, e.manager_id FROM employee e
JOIN emp_cte c ON e.manager_id = c.id
)
SELECT * FROM emp_cte;
29. What’s the difference between OLTP and OLAP systems? Which one is more suitable for real-time
applications?
OLTP is for day-to-day transactions — fast reads/writes like in banking. OLAP is for analysis and decision-
making, with complex queries on large data. OLTP is better suited for real-time applications.
30. Explain the concept of data warehousing. How does it help in business intelligence?
A data warehouse stores historical data from different sources. It helps in business intelligence by enabling fast
queries, analytics, and reporting — without impacting operational systems.
31. What is a star schema, and how is it different from a snowflake schema?
A star schema connects fact tables directly to dimension tables — it's simple and fast for queries. In a
snowflake schema, dimension tables are normalized into sub-tables — it's more structured but slightly
complex.
32. How do you perform ETL operations using SQL? Can you walk through the typical steps?
Sure. I extract data from sources using SELECT, transform it using joins, filters, or case statements, and then
load it into the destination tables. I usually automate this with scripts or stored procedures.
33. What are some best practices you follow for database security?
I follow the principle of least privilege, use parameterized queries to avoid SQL injection, encrypt sensitive
data, and make sure access is logged and audited regularly.
34. How would you implement role-based access control (RBAC) in SQL?
I’d create roles for different levels of access, assign permissions to those roles, and then map users to roles —
this way, access control is centralized and easy to manage.
35. What’s the difference between symmetric and asymmetric encryption? Where are they used in database
systems?
Symmetric encryption uses the same key for both encryption and decryption — it's fast, good for bulk data.
Asymmetric uses a public/private key pair — better for secure communication, like encrypting passwords or
keys.
36. How do you ensure data integrity in a distributed database setup?
I ensure data integrity using proper constraints, transaction handling where possible, synchronization between
nodes, and mechanisms like quorum-based writes or consistency checks depending on the system.

You might also like