What is SQL? SQL (Structured Query Language) is used for accessing and managing databases. It allows users to perform operations like data retrieval, insertion, update, and deletion.
What are the different types of SQL statements?
DDL (Data Definition Language):CREATE, ALTER, DROP
TCL (Transaction Control Language):COMMIT, ROLLBACK, SAVEPOINT
DCL (Data Control Language):GRANT, REVOKE
What is the difference between WHERE and HAVING clauses?
WHERE filters rows before grouping.
HAVING filters groups after grouping.
What is a Primary Key? A Primary Key uniquely identifies each row in a table and cannot contain NULL.
What is the difference between Primary Key and Unique Key?
Primary Key: Only one per table, cannot be NULL.
Unique Key: Multiple allowed, can have one NULL.
Intermediate SQL Questions
What is a Foreign Key? A Foreign Key is a field in one table that links to the Primary Key in another table, ensuring referential integrity.
What is the difference between INNER JOIN and OUTER JOIN?
INNER JOIN: Returns rows with matching values in both tables.
OUTER JOIN: Returns matching rows and non-matching rows from one or both tables (LEFT, RIGHT, or FULL).
What is the use of the GROUP BY clause? It groups rows with the same values in specified columns and allows aggregate functions (COUNT, SUM, etc.) to be applied.
What is normalization? Normalization organizes data to reduce redundancy and dependency by dividing a database into smaller tables.
What are SQL indexes, and why are they used? Indexes improve query performance by allowing faster data retrieval but can slow down INSERT and UPDATE operations.
Advanced SQL Questions
What is a Common Table Expression (CTE)? A CTE is a temporary result set defined within an SQL statement using WITH and can be referenced multiple times.
What is a Window Function in SQL? It performs calculations across rows related to the current row within a result set. Example: ROW_NUMBER(), RANK().
What is the difference between RANK() and DENSE_RANK()?
RANK(): Leaves gaps in ranking when values are tied.
DENSE_RANK(): No gaps in ranking.
Explain the difference between TRUNCATE and DELETE.
DELETE: Removes specific rows, can be rolled back.
TRUNCATE: Removes all rows, faster, cannot be rolled back.
What is a stored procedure? A stored procedure is a precompiled set of SQL statements that can be executed with a call.
Performance Optimization Questions
What are database partitions? Partitioning divides a table into smaller pieces for faster query processing.
What is a clustered index? A clustered index sorts and stores data rows in the table based on the indexed column.
How do you optimize a slow query?
Use indexes.
Avoid SELECT *.
Use proper WHERE clauses.
Analyze execution plans.
What is query execution plan? It is a visual representation of the steps taken by the database to execute a query.
What are materialized views? Materialized views store query results physically for faster access compared to regular views.
Real-World Scenario Questions
How would you find duplicate records in a table?sqlCopy codeSELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING COUNT(*) > 1;
How can you find the second-highest salary in a table?sqlCopy codeSELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
What is the difference between a view and a table?
View: A virtual table based on a query.
Table: A physical structure storing data.
How do you handle NULL values in SQL? Use IS NULL, IS NOT NULL, or functions like COALESCE() to replace NULL.
How do you implement transactions in SQL?sqlCopy codeBEGIN TRANSACTION; -- SQL operations COMMIT;
Latest SQL Features and Trends (2024 Focus)
What are JSON functions in SQL? Functions like JSON_VALUE() and JSON_ARRAYAGG() help handle JSON data.
What are Recursive CTEs? Recursive CTEs repeatedly reference themselves to process hierarchical or tree-structured data.
What are temporal tables? Temporal tables track data changes over time with historical data storage.
What is the difference between SQL and NoSQL databases?
SQL: Structured, relational, uses schemas.
NoSQL: Flexible, unstructured, document-based or key-value.
What is database sharding? Sharding distributes data across multiple servers to improve scalability and performance.
Miscellaneous
How do you ensure data integrity in SQL? Use constraints like PRIMARY KEY, FOREIGN KEY, CHECK, and UNIQUE.
What is the difference between UNION and UNION ALL?
UNION: Removes duplicates.
UNION ALL: Includes duplicates.
What is the use of the CASE statement in SQL? It provides conditional logic in queries.