SQL

SQL

Top Interview Questions

About SQL

SQL (Structured Query Language) is a standardized domain-specific language for managing relational databases. SQL enables creating, reading, updating, and deleting data (CRUD operations) and defining database structure. SQL is fundamental to most data-driven applications and information systems. SELECT queries retrieve data from tables, filtering rows with WHERE clauses, sorting with ORDER BY, and grouping with GROUP BY and HAVING. JOIN operations combine data from multiple tables based on relationships. Subqueries and CTEs (Common Table Expressions) enable complex query composition. INSERT statements add new data to tables. UPDATE statements modify existing data. DELETE statements remove data. Transactions ensure atomicity, guaranteeing that either all operations complete or none do, maintaining data consistency. Database design through normalization eliminates redundancy and ensures data integrity. Normalization progresses through normal forms (1NF, 2NF, 3NF, BCNF) progressively eliminating anomalies. Foreign key constraints maintain referential integrity between related tables. Indexes accelerate query performance by enabling fast data lookups. B-tree indexes work well for general queries. Hash indexes optimize equality comparisons. Index design significantly impacts query performance and must balance query speed against insert/update overhead. Views provide virtual tables enabling query abstraction and security through limited access to sensitive columns. Materialized views cache query results for performance. Stored procedures and functions encapsulate complex logic in the database. Aggregate functions like COUNT, SUM, AVG, MIN, and MAX summarize data across rows. Window functions compute aggregate and ranking functions within partitions of result sets. These functions enable powerful analytical queries. Performance tuning involves analyzing query execution plans, identifying slow queries, and optimizing indexes and queries. Statistics enable the query optimizer to choose efficient query plans. Monitoring query performance identifies bottlenecks. Transactions with isolation levels control concurrency between simultaneous queries. ACID properties guarantee data consistency. Row-level locking prevents conflicts. Different isolation levels trade consistency for concurrency. Triggers automatically execute code in response to data changes. Triggers enforce business rules and maintain audit trails. However, complex trigger logic can complicate debugging and performance analysis. Database replication maintains copies of data across servers for high availability and disaster recovery. Read replicas enable scaling read capacity. Master-slave and master-master replication architectures suit different requirements. Security controls limit data access through authentication, authorization, encryption, and auditing. Principle of least privilege limits users to necessary permissions. Data encryption protects sensitive information. Regular security audits identify vulnerabilities.