Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970s, SQL allows us to create, read, update, and delete data with simple yet effective commands. For both newcomers and seasoned professionals, mastering SQL is a must-have skill in today’s data-driven job market
In this article, we cover 100 SQL Interview Questions with answers asked in SQL developer interviews at MAANG and other high-paying companies. Whether we’re preparing for our first data-related role or seeking to advance our career, this guide will walk us through the most commonly asked SQL interview questions to help us stand out in competitive job interviews.

SQL Basic Interview Questions
“SQL Basic Interview Questions” covers fundamental concepts that are essential for anyone preparing for a SQL interview. Explore these essential questions to build a strong understanding and boost our confidence in SQL basics.
1. What is SQL?
SQL (Structured Query Language) is a standard programming language used to communicate with relational databases. It allows users to create, read, update, and delete data, and provides commands to define database schema and manage database security.
2. What is a database?
A database is an organized collection of data stored electronically, typically structured in tables with rows and columns. It is managed by a database management system (DBMS), which allows for efficient storage, retrieval, and manipulation of data.
3. What are the main types of SQL commands?
SQL commands are broadly classified into:
- DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE.
- DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE.
- DCL (Data Control Language): GRANT, REVOKE.
- TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT.
4. What is the difference between CHAR and VARCHAR2 data types?
- CHAR: Fixed-length storage. If the defined length is not fully used, it is padded with spaces.
- VARCHAR2: Variable-length storage. Only the actual data is stored, saving space when the full length is not needed.
5. What is a primary key?
A primary key is a unique identifier for each record in a table. It ensures that no two rows have the same value in the primary key column(s), and it does not allow NULL values.
6. What is a foreign key?
A foreign key is a column (or set of columns) in one table that refers to the primary key in another table. It establishes and enforces a relationship between the two tables, ensuring data integrity.
7. What is the purpose of the DEFAULT constraint?
The DEFAULT constraint assigns a default value to a column when no value is provided during an INSERT operation. This helps maintain consistent data and simplifies data entry.
8. What is normalization in databases?
Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. This involves dividing large tables into smaller, related tables and defining relationships between them to ensure consistency and avoid anomalies.
9. What is denormalization, and when is it used?
Denormalization is the process of combining normalized tables into larger tables for performance reasons. It is used when complex queries and joins slow down data retrieval, and the performance benefits outweigh the drawbacks of redundancy.
10. What is a query in SQL?
A query is a SQL statement used to retrieve, update, or manipulate data in a database. The most common type of query is a SELECT statement, which fetches data from one or more tables based on specified conditions.
11. What are the different operators available in SQL?
- Arithmetic Operators: +, -, *, /, %
- Comparison Operators: =, !=, <>, >, <, >=, <=
- Logical Operators: AND, OR, NOT
- Set Operators: UNION, INTERSECT, EXCEPT
- Special Operators: BETWEEN, IN, LIKE, IS NULL
12. What is a view in SQL?
A view is a virtual table created by a SELECT query. It does not store data itself, but presents data from one or more tables in a structured way. Views simplify complex queries, improve readability, and enhance security by restricting access to specific rows or columns.
13. What is the purpose of the UNIQUE constraint?
The UNIQUE constraint ensures that all values in a column (or combination of columns) are distinct. This prevents duplicate values and helps maintain data integrity.
14. What are the different types of joins in SQL?
- INNER JOIN: Returns rows that have matching values in both tables.
- LEFT JOIN (LEFT OUTER JOIN): Returns all rows from the left table, and matching rows from the right table.
- RIGHT JOIN (RIGHT OUTER JOIN): Returns all rows from the right table, and matching rows from the left table.
- FULL JOIN (FULL OUTER JOIN): Returns all rows when there is a match in either table.
- CROSS JOIN: Produces the Cartesian product of two tables.
15. What is the difference between INNER JOIN and OUTER JOIN?
- INNER JOIN: Returns only rows where there is a match in both tables.
- OUTER JOIN: Returns all rows from one table (LEFT, RIGHT, or FULL), and the matching rows from the other table. If there is no match, NULL values are returned for the non-matching side.
16. What is the purpose of the GROUP BY clause?
The GROUP BY clause is used to arrange identical data into groups. It is typically used with aggregate functions (such as COUNT, SUM, AVG) to perform calculations on each group rather than on the entire dataset.
17. What are aggregate functions in SQL?
Aggregate functions perform calculations on a set of values and return a single value. Common aggregate functions include:
- COUNT(): Returns the number of rows.
- SUM(): Returns the total sum of values.
- AVG(): Returns the average of values.
- MIN(): Returns the smallest value.
- MAX(): Returns the largest value.
18. What is a subquery?
A subquery is a query nested within another query. It is often used in the WHERE clause to filter data based on the results of another query, making it easier to handle complex conditions.
19. What is the difference between the WHERE and HAVING clauses?
- WHERE: Filters rows before any grouping takes place.
- HAVING: Filters grouped data after the GROUP BY clause has been applied.
In short, WHERE applies to individual rows, while HAVING applies to groups.
20. What are indexes, and why are they used?
Indexes are database objects that improve query performance by allowing faster retrieval of rows. They function like a book’s index, making it quicker to find specific data without scanning the entire table. However, indexes require additional storage and can slightly slow down data modification operations.
21. What is the difference between DELETE and TRUNCATE commands?
- DELETE: Removes rows one at a time and records each deletion in the transaction log, allowing rollback. It can have a WHERE clause.
- TRUNCATE: Removes all rows at once without logging individual row deletions. It cannot have a WHERE clause and is faster than DELETE for large data sets.
22. What is the purpose of the SQL ORDER BY clause?
The ORDER BY clause sorts the result set of a query in either ascending (default) or descending order, based on one or more columns. This helps present the data in a more meaningful or readable sequence.
23. What are the differences between SQL and NoSQL databases?
- SQL Databases:
- Use structured tables with rows and columns.
- Rely on a fixed schema.
- Offer ACID properties.
- NoSQL Databases:
- Use flexible, schema-less structures (e.g., key-value pairs, document stores).
- Are designed for horizontal scaling.
- Often focus on performance and scalability over strict consistency.
24. What is a table in SQL?
A table is a structured collection of related data organized into rows and columns. Columns define the type of data stored, while rows contain individual records.
25. What are the types of constraints in SQL?
Common constraints include:
- NOT NULL: Ensures a column cannot have NULL values.
- UNIQUE: Ensures all values in a column are distinct.
- PRIMARY KEY: Uniquely identifies each row in a table.
- FOREIGN KEY: Ensures referential integrity by linking to a primary key in another table.
- CHECK: Ensures that all values in a column satisfy a specific condition.
- DEFAULT: Sets a default value for a column when no value is specified.
26. What is a cursor in SQL?
A cursor is a database object used to retrieve, manipulate, and traverse through rows in a result set one row at a time. Cursors are helpful when performing operations that must be processed sequentially rather than in a set-based manner.
27. What is a trigger in SQL?
A trigger is a set of SQL statements that automatically execute in response to certain events on a table, such as INSERT, UPDATE, or DELETE. Triggers help maintain data consistency, enforce business rules, and implement complex integrity constraints.
28. What is the purpose of the SQL SELECT statement?
The SELECT statement retrieves data from one or more tables. It is the most commonly used command in SQL, allowing users to filter, sort, and display data based on specific criteria.
29. What are NULL values in SQL?
NULL represents a missing or unknown value. It is different from zero or an empty string. NULL values indicate that the data is not available or applicable.
30. What is a stored procedure?
A stored procedure is a precompiled set of SQL statements stored in the database. It can take input parameters, perform logic and queries, and return output values or result sets. Stored procedures improve performance and maintainability by centralizing business logic.
SQL Intermediate Interview Questions
This section covers moderately complex SQL topics like advanced queries, multi-table joins, subqueries, and basic optimization techniques. These questions help enhance skills for both database developers and administrators, preparing us for more technical SQL challenges in the field.
31. What is the difference between DDL and DML commands?
1. DDL (Data Definition Language):
These commands are used to define and modify the structure of database objects such as tables, indexes, and views. For example, the CREATE
command creates a new table, the ALTER
command modifies an existing table, and the DROP
command removes a table entirely. DDL commands primarily focus on the schema or structure of the database.
Example:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50)
);
2. DML (Data Manipulation Language):
These commands deal with the actual data stored within database objects. For instance, the INSERT
command adds rows of data to a table, the UPDATE
command modifies existing data, and the DELETE
command removes rows from a table. In short, DML commands allow you to query and manipulate the data itself rather than the structure.
Example:
INSERT INTO Employees (ID, Name) VALUES (1, 'Alice');
32. What is the purpose of the ALTER command in SQL?
The ALTER
command is used to modify the structure of an existing database object. This command is essential for adapting our database schema as requirements evolve.
- Add or drop a column in a table.
- Change a column’s data type.
- Add or remove constraints.
- Rename columns or tables.
- Adjust indexing or storage settings.
33. What is a composite primary key?
A composite primary key is a primary key made up of two or more columns. Together, these columns must form a unique combination for each row in the table. It’s used when a single column isn’t sufficient to uniquely identify a record.
Example:
Consider an Orders table where OrderID
and ProductID
together uniquely identify each record because multiple orders might include the same product, but not within the same order.
CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);
34. How is data integrity maintained in SQL databases?
Data integrity refers to the accuracy, consistency, and reliability of the data stored in the database. SQL databases maintain data integrity through several mechanisms:
- Constraints: Ensuring that certain conditions are always met. For example,
NOT NULL
ensures a column cannot have missing values, FOREIGN KEY
ensures a valid relationship between tables, and UNIQUE
ensures no duplicate values.
- Transactions: Ensuring that a series of operations either all succeed or all fail, preserving data consistency.
- Triggers: Automatically enforcing rules or validations before or after changes to data.
- Normalization: Organizing data into multiple related tables to minimize redundancy and prevent anomalies.
These measures collectively ensure that the data remains reliable and meaningful over time.
35. What are the advantages of using stored procedures?
- Improved Performance: Stored procedures are precompiled and cached in the database, making their execution faster than sending multiple individual queries.
- Reduced Network Traffic: By executing complex logic on the server, fewer round trips between the application and database are needed.
- Enhanced Security: Stored procedures can restrict direct access to underlying tables, allowing users to execute only authorized operations.
- Reusability and Maintenance: Once a procedure is written, it can be reused across multiple applications. If business logic changes, you only need to update the stored procedure, not every application that uses it.
36. What is a UNION operation, and how is it used?
The UNION
operator combines the result sets of two or more SELECT
queries into a single result set, removing duplicate rows. This is useful when we need a consolidated view of data from multiple tables or queries that have similar structure.
Example:
SELECT Name FROM Customers
UNION
SELECT Name FROM Employees;
37. What is the difference between UNION and UNION ALL?
- UNION: Removes duplicate rows from the result set, ensuring only unique rows are returned.
- UNION ALL: Includes all rows from each query, including duplicates.
- Performance-wise,
UNION ALL
is faster because it doesn’t require an additional step to remove duplicates.
Example:
SELECT Name FROM Customers
UNION ALL
SELECT Name FROM Employees;
38. How does the CASE statement work in SQL?
The CASE
statement is SQL’s way of implementing conditional logic in queries. It evaluates conditions and returns a value based on the first condition that evaluates to true. If no condition is met, it can return a default value using the ELSE
clause.
Example:
SELECT ID,
CASE
WHEN Salary > 100000 THEN 'High'
WHEN Salary BETWEEN 50000 AND 100000 THEN 'Medium'
ELSE 'Low'
END AS SalaryLevel
FROM Employees;
39. What are scalar functions in SQL?
Scalar functions operate on individual values and return a single value as a result. They are often used for formatting or converting data. Common examples include:
- LEN(): Returns the length of a string.
- ROUND(): Rounds a numeric value.
- CONVERT(): Converts a value from one data type to another.
Example:
SELECT LEN('Example') AS StringLength;
40. What is the purpose of the COALESCE function?
The COALESCE
function returns the first non-NULL value from a list of expressions. It’s commonly used to provide default values or handle missing data gracefully.
Example:
SELECT COALESCE(NULL, NULL, 'Default Value') AS Result;
41. What are the differences between SQL’s COUNT() and SUM() functions?
1. COUNT(): Counts the number of rows or non-NULL values in a column.
Example:
SELECT COUNT(*) FROM Orders;
2. SUM(): Adds up all numeric values in a column.
Example:
SELECT SUM(TotalAmount) FROM Orders;
42. What is the difference between the NVL and NVL2 functions?
- NVL(): Replaces a NULL value with a specified replacement value.
- NVL2(): Evaluates two values:
- If the first argument is NOT NULL, returns the second argument.
- If the first argument is NULL, returns the third argument.
Example:
SELECT NVL(Salary, 0) AS AdjustedSalary
FROM Employees;
43. How does the RANK() function differ from DENSE_RANK()?
- RANK(): Assigns a rank to each row, with gaps if there are ties.
- DENSE_RANK(): Assigns consecutive ranks without any gaps.
Example:
SELECT Name, Salary, RANK() OVER (ORDER BY Salary DESC) AS Rank
FROM Employees;
If two employees have the same salary, they get the same rank, but RANK()
will skip a number for the next rank, while DENSE_RANK()
will not.
44. What is the difference between ROW_NUMBER() and RANK()?
- ROW_NUMBER(): Assigns a unique number to each row regardless of ties.
- RANK(): Assigns the same number to tied rows and leaves gaps for subsequent ranks.
Example:
SELECT Name, ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNum
FROM Employees;
45. What are common table expressions (CTEs) in SQL?
A CTE is a temporary result set defined within a query. It improves query readability and can be referenced multiple times.
Example:
WITH TopSalaries AS (
SELECT Name, Salary
FROM Employees
WHERE Salary > 50000
)
SELECT * FROM TopSalaries WHERE Name LIKE 'A%';
46. What are window functions, and how are they used?
Window functions perform calculations across a set of rows that are related to the current row. Unlike aggregate functions, they don’t collapse the result set.
Example: Calculating a running total
SELECT Name, Salary, SUM(Salary) OVER (ORDER BY Salary) AS RunningTotal
FROM Employees;
47. What is the difference between an index and a key in SQL?
1. Index
- An index is a database object created to speed up data retrieval. It stores a sorted reference to table data, which helps the database engine find rows more quickly than scanning the entire table.
- Example: A non-unique index on a column like
LastName
allows quick lookups of rows where the last name matches a specific value.
2. Key
- A key is a logical concept that enforces rules for uniqueness or relationships in the data.
- For instance, a PRIMARY KEY uniquely identifies each row in a table and ensures that no duplicate or NULL values exist in the key column(s).
- A FOREIGN KEY maintains referential integrity by linking rows in one table to rows in another.
48. How does indexing improve query performance?
Indexing allows the database to locate and access the rows corresponding to a query condition much faster than scanning the entire table. Instead of reading each row sequentially, the database uses the index to jump directly to the relevant data pages. This reduces the number of disk I/O operations and speeds up query execution, especially for large tables.
Example:
CREATE INDEX idx_lastname ON Employees(LastName);
SELECT * FROM Employees WHERE LastName = 'Smith';
The index on LastName
lets the database quickly find all rows matching ‘Smith’ without scanning every record.
49. What are the trade-offs of using indexes in SQL databases?
Advantages
- Faster query performance, especially for SELECT queries with WHERE clauses, JOIN conditions, or ORDER BY clauses.
- Improved sorting and filtering efficiency.
Disadvantages:
- Increased storage space for the index structures.
- Additional overhead for write operations (INSERT, UPDATE, DELETE), as indexes must be updated whenever the underlying data changes.
- Potentially slower bulk data loads or batch inserts due to the need to maintain index integrity.
In short, indexes make read operations faster but can slow down write operations and increase storage requirements.
50. What is the difference between clustered and non-clustered indexes?
1. Clustered Index:
- Organizes the physical data in the table itself in the order of the indexed column(s).
- A table can have only one clustered index.
- Improves range queries and queries that sort data.
- Example: If
EmployeeID
is the clustered index, the rows in the table are stored physically sorted by EmployeeID
.
2. Non-Clustered Index:
- Maintains a separate structure that contains a reference (or pointer) to the physical data in the table.
- A table can have multiple non-clustered indexes.
- Useful for specific query conditions that aren’t related to the primary ordering of the data.
- Example: A non-clustered index on
LastName
allows fast lookups by last name even if the table is sorted by another column.
51. What are temporary tables, and how are they used?
Temporary tables are tables that exist only for the duration of a session or a transaction. They are useful for storing intermediate results, simplifying complex queries, or performing operations on subsets of data without modifying the main tables.
1. Local Temporary Tables:
- Prefixed with
#
(e.g., #TempTable
).
- Only visible to the session that created them.
- Automatically dropped when the session ends.
2. Global Temporary Tables:
- Prefixed with
##
(e.g., ##GlobalTempTable
).
- Visible to all sessions.
- Dropped when all sessions that reference them are closed.
Example:
CREATE TABLE #TempResults (ID INT, Value VARCHAR(50));
INSERT INTO #TempResults VALUES (1, 'Test');
SELECT * FROM #TempResults;
52. What is a materialized view, and how does it differ from a standard view?
- Standard View:
- A virtual table defined by a query.
- Does not store data; the underlying query is executed each time the view is referenced.
- A standard view shows real-time data.
- Materialized View:
- A physical table that stores the result of the query.
- Data is precomputed and stored, making reads faster.
- Requires periodic refreshes to keep data up to date.
- materialized view is used to store aggregated sales data, updated nightly, for fast reporting.
53. What is a sequence in SQL?
A sequence is a database object that generates a series of unique numeric values. It’s often used to produce unique identifiers for primary keys or other columns requiring sequential values.
Example:
CREATE SEQUENCE seq_emp_id START WITH 1 INCREMENT BY 1;
SELECT NEXT VALUE FOR seq_emp_id; -- Returns 1
SELECT NEXT VALUE FOR seq_emp_id; -- Returns 2
54. What are the advantages of using sequences over identity columns?
1. Greater Flexibility:
- Can specify start values, increments, and maximum values.
- Can be easily reused for multiple tables.
2. Dynamic Adjustment: Can alter the sequence without modifying the table structure.
3. Cross-Table Consistency: Use a single sequence for multiple related tables to ensure unique identifiers across them.
In short, sequences offer more control and reusability than identity columns.
55. How do constraints improve database integrity?
Constraints enforce rules that the data must follow, preventing invalid or inconsistent data from being entered:
- NOT NULL: Ensures that a column cannot contain NULL values.
- UNIQUE: Ensures that all values in a column are distinct.
- PRIMARY KEY: Combines NOT NULL and UNIQUE, guaranteeing that each row is uniquely identifiable.
- FOREIGN KEY: Ensures referential integrity by requiring values in one table to match primary key values in another.
- CHECK: Validates that values meet specific criteria (e.g.,
CHECK (Salary > 0)
).
By automatically enforcing these rules, constraints maintain data reliability and consistency.
56. What is the difference between a local and a global temporary table?
- Local Temporary Table:
- Prefixed with
#
(e.g., #TempTable
).
- Exists only within the session that created it.
- Automatically dropped when the session ends.
- Global Temporary Table:
- Prefixed with
##
(e.g., ##GlobalTempTable
).
- Visible to all sessions.
- Dropped only when all sessions referencing it are closed.
Example:
CREATE TABLE #LocalTemp (ID INT);
CREATE TABLE ##GlobalTemp (ID INT);
57. What is the purpose of the SQL MERGE statement?
The MERGE
statement combines multiple operations INSERT, UPDATE, and DELETE into one. It is used to synchronize two tables by:
- Inserting rows that don’t exist in the target table.
- Updating rows that already exist.
- Deleting rows from the target table based on conditions
Example:
MERGE INTO TargetTable T
USING SourceTable 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);
58. How can you handle duplicates in a query without using DISTINCT?
1. GROUP BY: Aggregate rows to eliminate duplicates
SELECT Column1, MAX(Column2)
FROM TableName
GROUP BY Column1;
2. ROW_NUMBER(): Assign a unique number to each row and filter by that
WITH CTE AS (
SELECT Column1, Column2, ROW_NUMBER() OVER (PARTITION BY Column1 ORDER BY Column2) AS RowNum
FROM TableName
)
SELECT * FROM CTE WHERE RowNum = 1;
59. What is a correlated subquery?
A correlated subquery is a subquery that references columns from the outer query. It is re-executed for each row processed by the outer query. This makes it more dynamic, but potentially less efficient.
Example:
SELECT Name,
(SELECT COUNT(*)
FROM Orders
WHERE Orders.CustomerID = Customers.CustomerID) AS OrderCount
FROM Customers;
60. What are partitioned tables, and when should we use them?
Partitioned tables divide data into smaller, more manageable segments based on a column’s value (e.g., date or region). Each partition is stored separately, making queries that target a specific partition more efficient. It is used when
- Large tables with millions or billions of rows.
- Scenarios where queries frequently filter on partitioned columns (e.g., year, region).
- To improve maintenance operations, such as archiving older partitions without affecting the rest of the table.
SQL Advanced Interview Questions
This section covers complex SQL topics, including performance tuning, complex indexing strategies, transaction isolation levels, and advanced query optimization techniques. By tackling these challenging questions, we’ll gain a deeper understanding of SQL, preparing us for senior-level roles and technical interviews.
61. What are the ACID properties of a transaction?
ACID is an acronym that stands for Atomicity, Consistency, Isolation, and Durability—four key properties that ensure database transactions are processed reliably.
1. Atomicity:
- A transaction is treated as a single unit of work, meaning all operations must succeed or fail as a whole.
- If any part of the transaction fails, the entire transaction is rolled back.
2. Consistency:
- A transaction must take the database from one valid state to another, maintaining all defined rules and constraints.
- This ensures data integrity is preserved throughout the transaction process.
3. Isolation:
- Transactions should not interfere with each other.
- Even if multiple transactions occur simultaneously, each must operate as if it were the only one in the system until it is complete.
4. Durability:
- Once a transaction is committed, its changes must persist, even in the event of a system failure.
- This ensures the data remains stable after the transaction is successfully completed.
62. What are the differences between isolation levels in SQL?
Isolation levels define the extent to which the operations in one transaction are isolated from those in other transactions. They are critical for managing concurrency and ensuring data integrity. Common isolation levels include:
1. Read Uncommitted:
- Allows reading uncommitted changes from other transactions.
- Can result in dirty reads, where a transaction reads data that might later be rolled back.
2. Read Committed:
- Ensures a transaction can only read committed data.
- Prevents dirty reads but does not protect against non-repeatable reads or phantom reads.
3. Repeatable Read:
- Ensures that if a transaction reads a row, that row cannot change until the transaction is complete.
- Prevents dirty reads and non-repeatable reads but not phantom reads.
4. Serializable:
- The highest level of isolation.
- Ensures full isolation by effectively serializing transactions, meaning no other transaction can read or modify data that another transaction is using.
- Prevents dirty reads, non-repeatable reads, and phantom reads, but may introduce performance overhead due to locking and reduced concurrency.
63. What is the purpose of the WITH (NOLOCK) hint in SQL Server?
- The WITH (NOLOCK) hint allows a query to read data without acquiring shared locks, effectively reading uncommitted data.
- It can improve performance by reducing contention for locks, especially on large tables that are frequently updated.
- Results may be inconsistent or unreliable, as the data read might change or be rolled back.
Example:
SELECT *
FROM Orders WITH (NOLOCK);
This query fetches data from the Orders
table without waiting for other transactions to release their locks.
64. How do you handle deadlocks in SQL databases?
Deadlocks occur when two or more transactions hold resources that the other transactions need, resulting in a cycle of dependency that prevents progress. Strategies to handle deadlocks include:
1. Deadlock detection and retry:
- Many database systems have mechanisms to detect deadlocks and terminate one of the transactions to break the cycle.
- The terminated transaction can be retried after the other transactions complete.
2. Reducing lock contention:
- Use indexes and optimized queries to minimize the duration and scope of locks.
- Break transactions into smaller steps to reduce the likelihood of conflicts.
3. Using proper isolation levels:
- In some cases, lower isolation levels can help reduce locking.
- Conversely, higher isolation levels (like Serializable) may ensure a predictable order of operations, reducing deadlock risk.
4. Consistent ordering of resource access:
- Ensure that transactions acquire resources in the same order to prevent cyclical dependencies.
65. What is a database snapshot, and how is it used?
A database snapshot is a read-only, static view of a database at a specific point in time.
- Reporting: Allowing users to query a consistent dataset without affecting live operations.
- Backup and recovery: Snapshots can serve as a point-in-time recovery source if changes need to be reversed.
- Testing: Providing a stable dataset for testing purposes without the risk of modifying the original data.
Example:
CREATE DATABASE MySnapshot ON
(
NAME = MyDatabase_Data,
FILENAME = 'C:\Snapshots\MyDatabase_Snapshot.ss'
)
AS SNAPSHOT OF MyDatabase;
66. What are the differences between OLTP and OLAP systems?
1. OLTP (Online Transaction Processing)
- Handles large volumes of simple transactions (e.g., order entry, inventory updates).
- Optimized for fast, frequent reads and writes.
- Normalized schema to ensure data integrity and consistency.
- Examples: e-commerce sites, banking systems.
2. OLAP (Online Analytical Processing)
- Handles complex queries and analysis on large datasets.
- Optimized for read-heavy workloads and data aggregation.
- Denormalized schema (e.g., star or snowflake schemas) to support faster querying.
- Examples: Business intelligence reporting, data warehousing.
67. What is a live lock, and how does it differ from a deadlock?
1. Live Lock
- Occurs when two or more transactions keep responding to each other’s changes, but no progress is made.
- Unlike a deadlock, the transactions are not blocked; they are actively running, but they cannot complete.
2. Deadlock
- Occurs when transactions are blocked waiting for each other to release locks.
- No progress can be made unless one of the transactions is terminated
68. What is the purpose of the SQL EXCEPT operator?
The EXCEPT
operator is used to return rows from one query’s result set that are not present in another query’s result set. It effectively performs a set difference, showing only the data that is unique to the first query.
Example:
SELECT ProductID FROM ProductsSold
EXCEPT
SELECT ProductID FROM ProductsReturned;
Use Case:
- To find discrepancies between datasets.
- To verify that certain data exists in one dataset but not in another.
Performance Considerations:
EXCEPT
works best when the datasets involved have appropriate indexing and when the result sets are relatively small.
- Large datasets without indexes may cause slower performance because the database has to compare each row.
69. How do you implement dynamic SQL, and what are its advantages and risks?
Dynamic SQL is SQL code that is constructed and executed at runtime rather than being fully defined and static. In SQL Server: Use sp_executesql
or EXEC.
In other databases: Concatenate query strings and execute them using the respective command for the database platform.
Syntax:
DECLARE @sql NVARCHAR(MAX)
SET @sql = ‘SELECT * FROM ‘ + @TableName
EXEC sp_executesql @sql;
Advantages:
- Flexibility: Dynamic SQL can adapt to different conditions, tables, or columns that are only known at runtime.
- Simplifies Complex Logic: Instead of writing multiple queries, a single dynamically constructed query can handle multiple scenarios.
Risks:
- SQL Injection Vulnerabilities: If user input is not sanitized, attackers can inject malicious SQL code.
- Performance Overhead: Because dynamic SQL is constructed at runtime, it may not benefit from cached execution plans, leading to slower performance.
- Complexity in Debugging: Dynamic queries can be harder to read and troubleshoot.
70. What is the difference between horizontal and vertical partitioning?
Partitioning is a database technique used to divide data into smaller, more manageable pieces.
- Horizontal Partitioning:
- Divides the rows of a table into multiple partitions based on values in a specific column.
- Example: Splitting a customer table into separate partitions by geographic region or by year.
- Use Case: When dealing with large datasets, horizontal partitioning can improve performance by limiting the number of rows scanned for a query.
- Vertical Partitioning:
- Divides the columns of a table into multiple partitions.
- Example: Storing infrequently accessed columns (e.g., large text or binary fields) in a separate table or partition.
- Use Case: Helps in optimizing storage and query performance by separating commonly used columns from less frequently accessed data.
- Key Difference:
- Horizontal partitioning is row-based, focusing on distributing the dataset’s rows across partitions.
- Vertical partitioning is column-based, aiming to separate less-used columns into different partitions or tables.
71. What are the considerations for indexing very large tables?
1. Indexing Strategy:
- Focus on the most frequently queried columns or those involved in JOIN and WHERE conditions.
- Avoid indexing every column, as it increases storage and maintenance costs.
2. Index Types:
- Use clustered indexes for primary key lookups and range queries.
- Use non-clustered indexes for filtering, ordering, and covering specific queries.
3. Partitioned Indexes:
- If the table is partitioned, consider creating local indexes for each partition. This improves manageability and can speed up queries targeting specific partitions.
4. Maintenance Overhead:
- Index rebuilding and updating can be resource-intensive. Plan for regular index maintenance during off-peak hours.
- Monitor index fragmentation and rebuild indexes as necessary to maintain performance.
5. Monitoring and Tuning:
- Continuously evaluate query performance using execution plans and statistics.
- Remove unused or rarely accessed indexes to reduce maintenance costs.
6. Indexing large tables requires a careful approach to ensure that performance gains from faster queries outweigh the costs of increased storage and maintenance effort.
72. What is the difference between database sharding and partitioning?
1. Sharding
- Sharding involves splitting a database into multiple smaller, independent databases (shards). Each shard operates on a subset of the overall data and can be hosted on separate servers.
- Sharding is a horizontal scaling strategy that distributes data across multiple databases, typically to handle massive data volumes and high traffic.
- Purpose: Horizontal scaling to handle large volumes of data and high query loads.
- Example: A global user database might be divided into shards by region, such as a shard for North America, Europe, and Asia.
- Key Benefit: Each shard can be queried independently, reducing the load on any single server.
2. Partitioning
- Partitioning splits a single table into smaller, logical pieces, usually within the same database.
- Partitioning is a logical organization of data within a single database to optimize performance and manageability.
- Purpose: Improve query performance by reducing the amount of data scanned, and simplify maintenance tasks such as archiving or purging old data.
- Example: A sales table could be partitioned by year so that queries targeting recent sales do not need to scan historical data.
73. What are the best practices for writing optimized SQL queries?
1. Write Simple, Clear Queries:
- Avoid overly complex joins and subqueries.
- Use straightforward, well-structured SQL that is easy to read and maintain.
2. Filter Data Early:
- Apply WHERE clauses as early as possible to reduce the amount of data processed.
- Consider using indexed columns in WHERE clauses for faster lookups.
3. **Avoid SELECT *:
- Retrieve only the columns needed. This reduces I/O and improves performance.
4. Use Indexes Wisely:
- Create indexes on columns that are frequently used in WHERE clauses, JOIN conditions, and ORDER BY clauses.
- Regularly review index usage and remove unused indexes.
5. Leverage Query Execution Plans:
- Use execution plans to identify bottlenecks, missing indexes, or inefficient query patterns.
6. Use Appropriate Join Types:
- Choose INNER JOIN, LEFT JOIN, or OUTER JOIN based on the data relationships and performance requirements.
7. Break Down Complex Queries:
- Instead of a single monolithic query, use temporary tables or CTEs to process data in stages.
8. Optimize Aggregations:
- Use GROUP BY and aggregate functions efficiently.
- Consider pre-aggregating data if queries frequently require the same computations.
9. Monitor Performance Regularly:
- Continuously analyze query performance and fine-tune as data volumes grow or usage patterns change.
74. How can you monitor query performance in a production database?
1. Use Execution Plans:
Review the execution plan of queries to understand how the database is retrieving data, which indexes are being used, and where potential bottlenecks exist.
2. Analyze Wait Statistics:
Identify where queries are waiting, such as on locks, I/O, or CPU, to pinpoint the cause of slowdowns.
3. Leverage Built-in Monitoring Tools:
- SQL Server: Use Query Store, DMVs (Dynamic Management Views), and performance dashboards.
- MySQL: Use
EXPLAIN
, SHOW PROFILE
, and the Performance Schema.
- PostgreSQL: Use
EXPLAIN (ANALYZE)
, pg_stat_statements
, and log-based monitoring.
4. Set Up Alerts and Baselines:
- Monitor key performance metrics (query duration, IOPS, CPU usage) and set thresholds.
- Establish baselines to quickly identify when performance degrades.
5. Continuous Query Tuning:
- Regularly revisit and tune queries as data grows or application requirements change.
- Remove unused or inefficient indexes and re-evaluate the indexing strategy.
75. What are the trade-offs of using indexing versus denormalization?
1. Indexing
- Advantages:
- Speeds up read operations and improves query performance without changing the data structure.
- Can be applied incrementally and is reversible if not effective.
- Consider indexing when you need faster lookups without altering the data model.
- Disadvantages:
- Slows down write operations as indexes need to be maintained.
- Requires additional storage.
2. Denormalization
- Advantages:
- Simplifies query logic by storing pre-joined or aggregated data.
- Can improve performance for read-heavy workloads where complex joins are frequent.
- Consider denormalization when complex joins or repeated aggregations significantly slow down queries
- Disadvantages:
- Introduces data redundancy, which can lead to inconsistencies.
- Increases storage requirements.
- Makes updates more complex, as redundant data must be synchronized.
76. How does SQL handle recursive queries?
SQL handles recursive queries using Common Table Expressions (CTEs). A recursive CTE repeatedly references itself to process hierarchical or tree-structured data.
Key Components:
- Anchor Member: The initial query that starts the recursion.
- Recursive Member: A query that references the CTE to continue building the result set.
- Termination Condition: Ensures that recursion stops after a certain depth or condition is met.
Example:
WITH RecursiveCTE (ID, ParentID, Depth) AS (
SELECT ID, ParentID, 1 AS Depth
FROM Categories
WHERE ParentID IS NULL
UNION ALL
SELECT c.ID, c.ParentID, r.Depth + 1
FROM Categories c
INNER JOIN RecursiveCTE r
ON c.ParentID = r.ID
)
SELECT * FROM RecursiveCTE;
77. What are the differences between transactional and analytical queries?
1. Transactional Queries:
- Focus on individual, short-term operations such as inserts, updates, and deletes.
- Optimize for high-throughput and low-latency.
- Often used in OLTP (Online Transaction Processing) systems.
2. Analytical Queries:
- Involve complex aggregations, multi-dimensional analysis, and data transformations.
- Typically read-heavy, processing large amounts of historical or aggregated data.
- Often used in OLAP (Online Analytical Processing) systems.
3. Key Differences:
- Transactional queries support day-to-day operations and maintain data integrity.
- Analytical queries support decision-making by providing insights from large datasets
78. How can you ensure data consistency across distributed databases?
1. Use Distributed Transactions: Implement two-phase commit (2PC) to ensure all participating databases commit changes simultaneously or roll back if any part fails.
2. Implement Eventual Consistency: If strong consistency isn’t required, allow data to become consistent over time. This approach is common in distributed systems where high availability is a priority.
3. Conflict Resolution Mechanisms: Use versioning, timestamps, or conflict detection rules to resolve inconsistencies.
4. Data Replication and Synchronization: Use reliable replication strategies to ensure that changes made in one database are propagated to others.
5. Regular Audits and Validation: Periodically verify that data remains consistent across databases and fix discrepancies as needed.
79. What is the purpose of the SQL PIVOT operator?
The PIVOT operator transforms rows into columns, making it easier to summarize or rearrange data for reporting.
Example:
Converting a dataset that lists monthly sales into a format that displays each month as a separate column.
SELECT ProductID, [2021], [2022]
FROM (
SELECT ProductID, YEAR(SaleDate) AS SaleYear, Amount
FROM Sales
) AS Source
PIVOT (
SUM(Amount)
FOR SaleYear IN ([2021], [2022])
) AS PivotTable;
80. What is a bitmap index, and how does it differ from a B-tree index?
1. Bitmap Index:
- Represents data with bitmaps (arrays of bits) to indicate the presence or absence of a value in each row.
- Efficient for low-cardinality columns, such as “gender” or “yes/no” fields.
- Can perform fast logical operations (AND, OR, NOT) on multiple columns simultaneously.
2. B-tree Index:
- Uses a balanced tree structure to store indexed data in a sorted order.
- Suitable for high-cardinality columns (e.g., unique identifiers, large ranges of values).
- Supports range-based queries efficiently.
3. Key Difference:
- Bitmap indexes excel with low-cardinality data and complex boolean conditions.
- B-tree indexes are better for unique or high-cardinality data and range queries.
Query Based SQL Interview Questions
This section is dedicated to questions that focus on writing and understanding SQL queries. By practicing these examples, we’ll learn how to retrieve, manipulate, and analyze data effectively, building the problem-solving skills needed for real-world scenarios.
81. Write a query to find the second-highest salary of an employee in a table.
SELECT MAX(Salary) AS SecondHighestSalary
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee);
Explanation:
This query identifies the second-highest salary by selecting the maximum salary that is less than the overall highest salary. The subquery determines the top salary, while the outer query finds the next highest value.
82. Write a query to retrieve employees who earn more than the average salary.
SELECT *
FROM Employee
WHERE Salary > (SELECT AVG(Salary) FROM Employee);
Explanation:
This query fetches details of employees whose salary exceeds the average salary. The subquery calculates the average salary, and the main query filters rows based on that result.
83. Write a query to fetch the duplicate values from a column in a table.
SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName
HAVING COUNT(*) > 1;
Explanation:
The query uses GROUP BY
to group identical values and HAVING COUNT(*) > 1
to identify values that appear more than once in the specified column.
84. Write a query to find the employees who joined in the last 30 days.
SELECT *
FROM Employee
WHERE JoiningDate > DATE_SUB(CURDATE(), INTERVAL 30 DAY);
Explanation:
By comparing the JoiningDate
to the current date minus 30 days, this query retrieves all employees who joined within the last month.
85. Write a query to fetch top 3 earning employees.
SELECT *
FROM Employee
ORDER BY Salary DESC
LIMIT 3;
Explanation:
The query sorts employees by salary in descending order and uses LIMIT 3
to return only the top three earners.
86. Write a query to delete duplicate rows in a table without using the ROWID keyword.
DELETE FROM Employee
WHERE EmployeeID NOT IN (
SELECT MIN(EmployeeID)
FROM Employee
GROUP BY Column1, Column2
);
Explanation:
This query retains only one row for each set of duplicates by keeping the row with the smallest EmployeeID
. It identifies duplicates using GROUP BY
and removes rows not matching the minimum ID.
87. Write a query to fetch common records from two tables.
SELECT *
FROM TableA
INNER JOIN TableB ON TableA.ID = TableB.ID;
Explanation:
An INNER JOIN
is used to find rows present in both tables by matching a common column (in this case, ID
).
88. Write a query to fetch employees whose names start and end with ‘A’.
SELECT *
FROM Employee
WHERE Name LIKE 'A%' AND Name LIKE '%A';
Explanation:
The query uses LIKE
with wildcard characters to filter rows where the Name
column starts and ends with the letter ‘A’.
89. Write a query to display all departments along with the number of employees in each.
SELECT DepartmentID, COUNT(*) AS EmployeeCount
FROM Employee
GROUP BY DepartmentID;
Explanation:
By grouping employees by their DepartmentID
and counting rows in each group, the query produces a list of departments along with the employee count.
90. Write a query to find employees who do not have managers.
SELECT *
FROM Employee
WHERE ManagerID IS NULL;
Explanation:
This query selects employees whose ManagerID
column is NULL
, indicating they don’t report to a manager.
91. Write a query to fetch the 3rd and 4th highest salaries.
SELECT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 2 OFFSET 2;
Explanation:
Using ORDER BY
and the combination of LIMIT
and OFFSET
, the query skips the top two salaries and retrieves the next two highest.
92. Write a query to transpose rows into columns.
SELECT
MAX(CASE WHEN ColumnName = 'Condition1' THEN Value END) AS Column1,
MAX(CASE WHEN ColumnName = 'Condition2' THEN Value END) AS Column2
FROM TableName;
Explanation:
This query converts specific row values into columns using conditional aggregation with CASE
. Each column’s value is determined based on a condition applied to rows.
93. Write a query to fetch records updated within the last hour.
SELECT *
FROM TableName
WHERE UpdatedAt >= NOW() - INTERVAL 1 HOUR;
Explanation:
By comparing the UpdatedAt
timestamp to the current time minus one hour, the query retrieves rows updated in the last 60 minutes.
94. Write a query to list employees in departments that have fewer than 5 employees.
SELECT *
FROM Employee
WHERE DepartmentID IN (
SELECT DepartmentID
FROM Employee
GROUP BY DepartmentID
HAVING COUNT(*) < 5
);
Explanation:
The subquery counts employees in each department, and the main query uses those results to find employees working in departments with fewer than 5 members.
95. Write a query to check if a table contains any records.
SELECT CASE
WHEN EXISTS (SELECT * FROM TableName) THEN 'Has Records'
ELSE 'No Records'
END AS Status;
Explanation:
The query uses EXISTS
to determine if any rows exist in the table, returning a status of ‘Has Records’ or ‘No Records’ based on the result.
96. Write a query to find employees whose salaries are higher than their managers.
SELECT e.EmployeeID, e.Salary
FROM Employee e
JOIN Employee m ON e.ManagerID = m.EmployeeID
WHERE e.Salary > m.Salary;
Explanation:
This query joins the Employee
table with itself to compare employee salaries to their respective managers’ salaries, selecting those who earn more.
97. Write a query to fetch alternating rows from a table.
SELECT *
FROM Employee
WHERE MOD(RowID, 2) = 0;
Explanation:
By applying a modulo operation on a unique identifier (RowID
), the query returns rows with even IDs, effectively fetching every other row.
98. Write a query to find departments with the highest average salary.
SELECT DepartmentID
FROM Employee
GROUP BY DepartmentID
ORDER BY AVG(Salary) DESC
LIMIT 1;
Explanation:
Grouping by DepartmentID
and ordering by the average salary in descending order, the query returns the department with the highest average.
99. Write a query to fetch the nth record from a table.
SELECT *
FROM Employee
LIMIT 1 OFFSET (n-1);
Explanation:
Using LIMIT 1 OFFSET (n-1)
, this query retrieves a single row corresponding to the specified position (nth) in the ordered result set.
100. Write a query to find employees hired in the same month of any year.
SELECT *
FROM Employee
WHERE MONTH(JoiningDate) = MONTH(CURDATE());
Explanation:
By comparing the month of JoiningDate
to the current month, the query selects all employees who were hired in that month regardless of the year.
Conclusion
A solid understanding of SQL is essential for anyone aspiring to work in data analysis, database administration, or software development. By reviewing and practicing these 100 SQL interview questions, we will gain the confidence and knowledge needed to answer even the toughest queries during our next interview. Remember, preparation is the key because each question we master brings us closer to securing that dream job in the tech industry.
Similar Reads
SQL Tutorial
SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases. In this
11 min read
SQL Basics
What is Database?
In todayâs data-driven world, databases are indispensable for managing, storing, and retrieving information efficiently. From small-scale businesses to global enterprises, databases serve as the backbone of operations, powering applications, websites, and analytics systems. In this comprehensive art
15 min read
Types of Databases
Databases are essential for storing and managing data in todayâs digital world. They serve as the backbone of various applications, from simple personal projects to complex enterprise systems. Understanding the different types of databases is crucial for choosing the right one based on specific requ
9 min read
Introduction of DBMS (Database Management System)
A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read
Non-Relational Databases and Their Types
In the area of database management, the data is arranged in two ways which are Relational Databases (SQL) and Non-Relational Databases (NoSQL). While relational databases organize data into structured tables, non-relational databases use various flexible data models like key-value pairs, documents,
7 min read
What is SQL?
SQL stands for Structured Query Language. It is a standardized programming language used to manage and manipulate relational databases. It enables users to perform a variety of tasks such as querying data, creating and modifying database structures, and managing access permissions. SQL is widely use
11 min read
SQL Data Types
SQL Data Types are very important in relational databases. It ensures that data is stored efficiently and accurately. Data types define the type of value a column can hold, such as numbers, text, or dates. Understanding SQL Data Types is critical for database administrators, developers, and data ana
5 min read
SQL Operators
SQL operators are important in database management systems (DBMS) as they allow us to manipulate and retrieve data efficiently. Operators in SQL perform arithmetic, logical, comparison, bitwise, and other operations to work with database values. Understanding SQL operators is crucial for performing
6 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are essential for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will
7 min read
Create Database in SQL
SQL CREATE DATABASE
Creating a database is one of the fundamental tasks in SQL and is the first step in structuring your data for efficient management. Whether you're a developer or a database administrator, understanding the CREATE DATABASE statement is essential. Understanding how to use this command effectively is c
6 min read
SQL DROP DATABASE
The SQL DROP DATABASE statement is an important command used to permanently delete a database from the Database Management System (DBMS). When executed, this command removes the database and all its associated objects, including tables, views, stored procedures, and other entities. In this article,
4 min read
SQL Query to Rename Database
Renaming a database in SQL is an essential task that database administrators and developers frequently perform. Whether youâre reorganizing your data, correcting naming conventions, or simply updating your project structure, knowing how to rename a database properly is critical. In this article, we'
3 min read
SQL Select Database
The USE DATABASE statement is a command in certain SQL-based database management systems that allows users to select and set a specific database as the default for the current session. By selecting a database, subsequent queries are executed within the context of that database, making it easier to i
3 min read
Tables in SQL
SQL CREATE TABLE
The SQL CREATE TABLE statement is a foundational command used to define and structure a new table in a database. By specifying the columns, data types, and constraints such as PRIMARY KEY, NOT NULL, and CHECK, helps you design the database schema. In this article, we'll learn the syntax, best practi
4 min read
SQL DROP TABLE
The DROP TABLE command in SQL is a powerful and essential tool used to permanently delete a table from a database, along with all of its data, structure, and associated constraints such as indexes, triggers, and permissions. When executed, this command removes the table and all its contents, making
4 min read
SQL DELETE Statement
The SQL DELETE statement is one of the most commonly used commands in SQL (Structured Query Language). It allows you to remove one or more rows from the table depending on the situation. Unlike the DROP statement, which removes the entire table, the DELETE statement removes data (rows) from the tabl
4 min read
ALTER (RENAME) in SQL
In SQL, making structural changes to a database is often necessary. Whether it's renaming a table or a column, adding new columns, or modifying data types, the SQL ALTER TABLE command plays a critical role. This command provides flexibility to manage and adjust database schemas without affecting the
4 min read
DROP and TRUNCATE in SQL
The DROP and TRUNCATE commands in SQL are used to remove data from a table, but they work differently. Understanding the difference between these two commands is important for proper database management, especially when dealing with large amounts of data. This article provides an in-depth explanatio
5 min read
SQL Query to Create a Backup Table
Relational databases play an important role in managing data, especially during complex operations like updates and deletions. To maintain data integrity, it is essential to back up tables before making changes. SQL backup tables ensure the safety of the original dataset, allow for data recovery, an
5 min read
What is Temporary Table in SQL?
A temporary table in SQL is an important tool for maintaining intermediate results during query execution. They help store temporary data without affecting the underlying permanent tables. In this article, weâll explore temporary tables in SQL, their types (local vs. global), and how to use them eff
3 min read
SQL ALTER TABLE
The SQL ALTER TABLE statement is a powerful tool that allows you to modify the structure of an existing table in a database. Whether you're adding new columns, modifying existing ones, deleting columns, or renaming them, the ALTER TABLE statement enables you to make changes without losing the data s
5 min read
SQL Queries
SQL SELECT Query
The select query in SQL is one of the most commonly used SQL commands to retrieve data from a database. With the select command in SQL, users can access data and retrieve specific records based on various conditions, making it an essential tool for managing and analyzing data. In this article, weâll
4 min read
SQL TOP, LIMIT, FETCH FIRST Clause
SQL TOP, LIMIT, and FETCH FIRST clauses are used to retrieve a specific number of records from a table. These clauses are especially useful in large datasets with thousands of records. Each of these SQL clauses performs a similar operation of limiting the results returned by a query, but different d
8 min read
SQL SELECT FIRST
The SELECT FIRST clause is used in some SQL databases (primarily MS Access) to retrieve the first record from a table based on the order in which data is stored or queried. It is commonly used when you need to access just one entry, such as the first row based on the natural order or after sorting b
3 min read
SQL - SELECT LAST
SELECT LAST is a concept or function often used to describe retrieving the last record or last row from a table in SQL. Although MS Access supports a LAST() function to directly fetch the last value from a column, this function is not universally supported across all SQL-based databases. Instead, in
5 min read
SQL - SELECT RANDOM
In SQL, the RANDOM() function is used to fetch random rows from a table. It is an extremely useful function in various applications, such as selecting random users, retrieving random questions from a pool, or even for random sampling in data analysis. In this article, we will explore how to use RAND
4 min read
SQL SELECT IN Statement
The IN operator in SQL is used to compare a column's value against a set of values. It returns TRUE if the column's value matches any of the values in the specified list, and FALSE if there is no match. In this article, we will learn how IN operator works and provide practical examples to help you b
4 min read
SQL - SELECT DATE
SQL (Structured Query Language) can work with datetime data types. SELECT DATE is an important concept when retrieving records that filter data based on date. Whether you work with employee records, transaction records, or product sales, modifying and retrieving date data is often important for your
3 min read
SQL Query to Insert Multiple Rows
In SQL, the INSERT statement is used to add new records to a database table. When you need to insert multiple rows in a single query, the INSERT statement becomes efficient. In this article, We will learn different methods such as using basic INSERT statements, utilizing INSERT INTO ... SELECT for b
4 min read
SQL INSERT INTO Statement
The SQL INSERT INTO statement is one of the most commonly used commands for adding new data into a table in a database. Whether you're working with customer data, products, or user details, mastering this command is crucial for efficient database management. Letâs break down how this command works,
6 min read
SQL UPDATE Statement
In SQL, the UPDATE statement is used to modify existing records in a table. Whether you are updating a single record or multiple records at once, SQL provides the necessary functionality to make these changes. Whether you are working with a small dataset or handling large-scale databases, the UPDATE
6 min read
SQL DELETE Statement
The SQL DELETE statement is one of the most commonly used commands in SQL (Structured Query Language). It allows you to remove one or more rows from the table depending on the situation. Unlike the DROP statement, which removes the entire table, the DELETE statement removes data (rows) from the tabl
4 min read
SQL Query to Delete Duplicate Rows
Duplicate rows in a database can cause inaccurate results, waste storage space, and slow down queries. Cleaning duplicate records from our database is an essential maintenance task for ensuring data accuracy and performance. Duplicate rows in a SQL table can lead to data inconsistencies and performa
5 min read
SQL Clauses
SQL | WHERE Clause
The SQL WHERE clause allows to filtering of records in queries. Whether you're retrieving data, updating records, or deleting entries from a database, the WHERE clause plays an important role in defining which rows will be affected by the query. Without it, SQL queries would return all rows in a tab
4 min read
SQL | WITH Clause
SQL queries can sometimes be complex, especially when you need to deal with multiple nested subqueries, aggregations, and joins. This is where the SQL WITH clause also known as Common Table Expressions (CTEs) comes in to make life easier. The WITH Clause is a powerful tool that simplifies complex SQ
6 min read
SQL HAVING Clause with Examples
The HAVING clause in SQL is used to filter query results based on aggregate functions. Unlike the WHERE clause, which filters individual rows before grouping, the HAVING clause filters groups of data after aggregation. It is commonly used with functions like SUM(), AVG(), COUNT(), MAX(), and MIN().
4 min read
SQL ORDER BY
The ORDER BY clause in SQL is a powerful feature used to sort query results in either ascending or descending order based on one or more columns. Whether you're presenting data to users or analyzing large datasets, sorting the results in a structured way is essential. In this article, weâll explain
5 min read
SQL | GROUP BY
The GROUP BY statement in SQL is used for organizing and summarizing data based on identical values in specified columns. By using the GROUP BY clause, users can apply aggregate functions like SUM, COUNT, AVG, MIN, and MAX to each group, making it easier to perform detailed data analysis. In this ar
5 min read
SQL LIMIT Clause
The LIMIT clause in SQL is used to control the number of rows returned in a query result. It is particularly useful when working with large datasets, allowing you to retrieve only the required number of rows for analysis or display. Whether we're looking to paginate results, find top records, or jus
4 min read
SQL Operators
SQL AND and OR Operators
The SQL AND and OR operators are used to filter data based on multiple conditions. These logical operators allow users to retrieve precise results from a database by combining various conditions in SELECT, INSERT, UPDATE, and DELETE statements. In this article, we'll learn the AND and OR operators,
3 min read
SQL AND and OR Operators
The SQL AND and OR operators are used to filter data based on multiple conditions. These logical operators allow users to retrieve precise results from a database by combining various conditions in SELECT, INSERT, UPDATE, and DELETE statements. In this article, we'll learn the AND and OR operators,
3 min read
SQL LIKE Operator
The SQL LIKE operator is used for performing pattern-based searches in a database. It is used in combination with the WHERE clause to filter records based on specified patterns, making it essential for any database-driven application that requires flexible search functionality. In this article, we w
5 min read
SQL IN Operator
The SQL IN operator filters data based on a list of specific values. In general, we can only use one condition in the Where clause, but the IN operator allows us to specify multiple values. In this article, we will learn about the IN operator in SQL by understanding its syntax and examples. IN Opera
4 min read
SQL NOT Operator
The SQL NOT Operator is a logical operator used to negate or reverse the result of a condition in SQL queries. It is commonly used with the WHERE clause to filter records that do not meet a specified condition, helping you exclude certain values from your results. In this article, we will learn ever
3 min read
SQL NOT EQUAL Operator
The SQL NOT EQUAL operator is a comparison operator used to check if two expressions are not equal to each other. It helps filter out records that match certain conditions, making it a valuable tool in SQL queries. In this article, We will explore the SQL NOT EQUAL operator, including its syntax, us
4 min read
SQL IS NULL
The SQL IS NULL operator is a logical operator used to identify and filter out rows with NULL values in a column. A NULL value represents missing or undefined data in a database. It is different from a zero value or blank space, and it indicates that the value is unknown. In this article, we will le
4 min read
SQL UNION Operator
The SQL UNION operator is used to combine the result sets of two or more SELECT queries into a single result set. It is a powerful tool in SQL that helps aggregate data from multiple tables, especially when the tables have similar structures. In this guide, we'll explore the SQL UNION operator, how
4 min read
SQL UNION ALL
UNION ALL Operator is used to combine the results of two or more SELECT statements into a single result set. Unlike the UNION operator, which eliminates duplicate records and UNION ALL includes all duplicates. This makes UNION ALL it faster and more efficient when we don't need to remove duplicates.
4 min read
SQL | Except Clause
The SQL EXCEPT operator is used to return the rows from the first SELECT statement that are not present in the second SELECT statement. This operator is conceptually similar to the subtract operator in relational algebra. It is particularly useful for excluding specific data from your result set. In
4 min read
SQL BETWEEN Operator
The BETWEEN operator in SQL is used to filter records within a specific range. Whether applied to numeric, text, or date columns it simplifies the process of retrieving data that falls within a particular boundary. In this article, we will explore the SQL BETWEEN operator with examples. SQL BETWEEN
3 min read
SQL | ALL and ANY
In SQL, the ALL and ANY operators are logical operators used to compare a value with a set of values returned by a subquery. These operators provide powerful ways to filter results based on a range of conditions. In this article, we will explore ALL and ANY in SQL, their differences, and how to use
4 min read
SQL | ALL and ANY
In SQL, the ALL and ANY operators are logical operators used to compare a value with a set of values returned by a subquery. These operators provide powerful ways to filter results based on a range of conditions. In this article, we will explore ALL and ANY in SQL, their differences, and how to use
4 min read
SQL | INTERSECT Clause
In SQL, the INTERSECT clause is used to retrieve the common records between two SELECT queries. It returns only the rows that are present in both result sets. This makes INTERSECT an essential clause when we need to find overlapping data between two or more queries. In this article, we will explain
5 min read
SQL | EXISTS
The SQL EXISTS condition is used to test whether a correlated subquery returns any results. If the subquery returns at least one row, the EXISTS condition evaluates to TRUE; otherwise, it evaluates to FALSE. The EXISTS operator can be used in various SQL statements like SELECT, UPDATE, INSERT, and D
3 min read
SQL CASE Statement
The CASE statement in SQL is a versatile conditional expression that enables us to incorporate conditional logic directly within our queries. It allows you to return specific results based on certain conditions, enabling dynamic query outputs. Whether you need to create new columns, modify existing
4 min read
SQL Aggregate Functions
SQL Aggregate functions
SQL Aggregate Functions are used to perform calculations on a set of rows and return a single value. They are often used with the GROUP BY clause in SQL to summarize data for each group. Commonly used aggregate functions include COUNT(), SUM(), AVG(), MIN(), and MAX(). In this article, we'll learn t
3 min read
SQL COUNT(), AVG() and SUM() Function
SQL aggregate functions, such as COUNT(), AVG(), and SUM(), are essential tools for performing mathematical analysis on data. These functions allow you to gather valuable insights from your database, such as calculating totals, and averages, and counting specific rows. In this article, weâll explain
3 min read
SQL COUNT(), AVG() and SUM() Function
SQL aggregate functions, such as COUNT(), AVG(), and SUM(), are essential tools for performing mathematical analysis on data. These functions allow you to gather valuable insights from your database, such as calculating totals, and averages, and counting specific rows. In this article, weâll explain
3 min read
SQL MIN() and MAX() Functions
The SQL MIN() and MAX() functions are essential aggregate functions in SQL used for data analysis. They allow you to extract the minimum and maximum values from a specified column, respectively, making them invaluable when working with numerical, string, or date-based data. In this article, we will
4 min read
SQL MIN() and MAX() Functions
The SQL MIN() and MAX() functions are essential aggregate functions in SQL used for data analysis. They allow you to extract the minimum and maximum values from a specified column, respectively, making them invaluable when working with numerical, string, or date-based data. In this article, we will
4 min read
SQL COUNT(), AVG() and SUM() Function
SQL aggregate functions, such as COUNT(), AVG(), and SUM(), are essential tools for performing mathematical analysis on data. These functions allow you to gather valuable insights from your database, such as calculating totals, and averages, and counting specific rows. In this article, weâll explain
3 min read
SQL Data Constraints
SQL NOT NULL Constraint
In SQL, constraints are used to enforce rules on data, ensuring the accuracy, consistency, and integrity of the data stored in a database. One of the most commonly used constraints is the NOT NULL constraint, which ensures that a column cannot have NULL values. This is important for maintaining data
3 min read
SQL | UNIQUE Constraint
In SQL, constraints play a vital role in maintaining the integrity and accuracy of the data stored in a database. One such constraint is the UNIQUE constraint, which ensures that all values in a column (or a combination of columns) are distinct, preventing duplicate entries. This constraint is espec
4 min read
SQL PRIMARY KEY Constraint
The PRIMARY KEY constraint in SQL is one of the most important constraints used to ensure data integrity in a database table. A primary key uniquely identifies each record in a table, preventing duplicate or NULL values in the specified column(s). Understanding how to properly implement and use the
5 min read
SQL FOREIGN KEY Constraint
A FOREIGN KEY constraint is a fundamental concept in relational databases, ensuring data integrity by enforcing relationships between tables. By linking a child table to a parent table, the foreign key establishes referential integrity. This constraint ensures that the values in the foreign key colu
5 min read
Composite Key in SQL
A composite key is a primary key that is made up of more than one column to uniquely identify records in a table. Unlike a single-column primary key, a composite key combines two or more columns to ensure uniqueness. While any of the individual columns in a composite key might not be unique on their
2 min read
SQL | UNIQUE Constraint
In SQL, constraints play a vital role in maintaining the integrity and accuracy of the data stored in a database. One such constraint is the UNIQUE constraint, which ensures that all values in a column (or a combination of columns) are distinct, preventing duplicate entries. This constraint is espec
4 min read
SQL - ALTERNATE KEY
Alternate Key is any candidate key not selected as the primary key. So, while a table may have multiple candidate keys (sets of columns that could uniquely identify rows), only one of them is designated as the Primary Key. The rest of these candidate keys become Alternate Keys. In other words, we ca
4 min read
SQL | CHECK Constraint
In SQL, One such constraint is the CHECK constraint, which allows to enforcement of domain integrity by limiting the values that can be inserted or updated in a column. By using CHECK, we can define conditions on a columnâs values and ensure that they adhere to specific rules. In this article, we wi
5 min read
SQL | DEFAULT Constraint
In SQL, maintaining data integrity and ensuring consistency across tables is important for effective database management. One way to achieve this is by using constraints. Among the many types of constraints, the DEFAULT constraint plays an important role in automating data insertion and ensuring tha
3 min read
SQL Joining Data
SQL Joins (Inner, Left, Right and Full Join)
SQL joins are the foundation of database management systems, enabling the combination of data from multiple tables based on relationships between columns. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understandi
6 min read
SQL Outer Join
SQL Outer Joins allow retrieval of rows from two or more tables based on a related column. Unlike inner Joins, they also include rows that do not have a corresponding match in one or both of the tables. This capability makes Outer Joins extremely useful for comprehensive data analysis and reporting,
4 min read
SQL LEFT JOIN
In SQL, LEFT JOIN retrieves all records from the left table and only the matching records from the right table. When there is no matching record found, NULL values are returned for columns from the right table. This makes LEFT JOIN extremely useful for queries where you need to retain all records fr
5 min read
SQL RIGHT JOIN
In SQL, the RIGHT JOIN (also called RIGHT OUTER JOIN) is an essential command used to combine data from two tables based on a related column. It returns all records from the right table, along with the matching records from the left table. If there is no matching record in the left table, SQL will r
4 min read
SQL FULL JOIN
In SQL, the FULL JOIN (or FULL OUTER JOIN) is a powerful technique used to combine records from two or more tables. Unlike an INNER JOIN, which only returns rows where there are matches in both tables, a FULL JOIN retrieves all rows from both tables, filling in NULL values where matches do not exist
4 min read
SQL CROSS JOIN
In SQL, the CROSS JOIN is a unique join operation that returns the Cartesian product of two or more tables. This means it matches each row from the left table with every row from the right table, resulting in a combination of all possible pairs of records. In this article, we will learn the CROSS JO
3 min read
SQL Self Join
A Self Join in SQL is a powerful technique that allows one to join a table with itself. This operation is helpful when you need to compare rows within the same table based on specific conditions. A Self Join is often used in scenarios where there is hierarchical or relational data within the same ta
4 min read
SQL | UPDATE with JOIN
In SQL, the UPDATE with JOIN statement is a powerful tool that allows updating one table using data from another table based on a specific JOIN condition. This technique is particularly useful when we need to synchronize data, merge records, or update specific columns in one table by referencing rel
4 min read
SQL DELETE JOIN
The SQL DELETE JOIN statement is a powerful feature that allows us to delete rows from one table based on conditions involving another table. This is particularly useful when managing relationships between tables in a database. For example, we may want to delete rows in a "Library Books" table where
4 min read
Recursive Join in SQL
In SQL, a recursive join is a powerful technique used to handle hierarchical data relationships, such as managing employee-manager relationships, family trees, or any data with a self-referential structure. This type of join enables us to combine data from the same table repeatedly, accumulating rec
3 min read