SQL Interview questions 2023
Updated: Jul 16
why we require right outer join if we can flip table with left outer join?
The choice between a right outer join and flipping the tables with a left outer join depends on the specific scenario and the desired result.
In a typical scenario, you would use a left outer join when you want to keep all the records from the left table and include matching records from the right table. This means that any unmatched records from the right table would be excluded from the result.
On the other hand, a right outer join is used when you want to keep all the records from the right table and include matching records from the left table. This means that any unmatched records from the left table would be excluded from the result.
Flipping the tables and using a left outer join instead of a right outer join can work in some cases, but it's not always a feasible solution. Here are a few reasons why a right outer join might be necessary:
Data availability: The decision to use a right outer join or left outer join depends on the availability and importance of data in the tables. If the right table contains crucial information that needs to be included in the result, a right outer join would be appropriate. Flipping the tables and using a left outer join may not yield the desired result if the essential information is in the right table.
Data structure and relationships: The choice between a right outer join and flipping the tables also depends on the structure and relationships between the tables. Sometimes the tables may have different structures or relationships, and flipping them may not be straightforward or practical. In such cases, using a right outer join is a more appropriate solution.
Clarity and readability: Using the appropriate join type improves the clarity and readability of your queries or data transformations. Choosing a right outer join when you want to keep all the records from the right table communicates your intent more clearly to other users who might review or work with your code.
Please explain sql window function in simple language ?
SQL window functions are a way to perform calculations on a specific subset or "window" of rows in a result set. These functions operate on a set of rows that are related to the current row based on a defined criteria, such as a specific order or partition.
In simple terms, you can think of a window function as a way to calculate values for each row in a result set based on a group of related rows.
Here's a step-by-step explanation of how window functions work:
Partitioning: First, the rows in the result set are divided into partitions or groups based on a specified column or expression. Each partition acts as a separate subset of rows.
Ordering: Within each partition, the rows are then sorted in a specific order based on one or more columns.
Window Frame: The window frame defines the range of rows used for calculations. It specifies which rows are included in the calculation by referencing a certain number of rows before and after the current row or within a specific range.
Calculation: The window function is applied to the rows within the defined window frame. It performs calculations on the values in the window and returns a result for each row.
Result Set: The window function result is added as a new column to the result set, alongside the original columns.
Window functions can be used for various calculations, such as calculating running totals, finding ranks or percentiles within a partition, computing moving averages, and more. They provide a way to perform complex calculations without the need for subqueries or temporary tables.
To summarize, SQL window functions allow you to perform calculations on specific groups or windows of rows in a result set, providing powerful analytical capabilities within a single SQL query.
Explain with example of all window functions in SQL?
ROW_NUMBER(): This function assigns a unique sequential number to each row within a partition. SELECT ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num, employee_name, salary FROM employees; This query assigns a row number to each employee based on their salary in descending order.
RANK(): This function calculates the rank of each row within a partition. Rows with the same values receive the same rank, and the next rank is skipped. SELECT RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank, employee_name, salary, department FROM employees; Here, the rank of each employee is calculated within their department based on their salary in descending order.
DENSE_RANK(): This function calculates the rank of each row within a partition. Rows with the same values receive the same rank, and the next rank is not skipped. SELECT DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS dense_rank, employee_name, salary, department FROM employees; Similar to the RANK() function, but the dense_rank assigns consecutive ranks without skipping any numbers.
NTILE(): This function divides the rows within a partition into a specified number of buckets or groups. SELECT NTILE(4) OVER (ORDER BY score) AS quartile, student_name, score FROM students; This query divides students into four quartiles based on their scores.
LAG(): This function provides access to a previous row's value within the same partition. SELECT employee_name, salary, LAG(salary) OVER (ORDER BY hire_date) AS previous_salary FROM employees; The LAG() function retrieves the previous row's salary for each employee based on the order of their hire date.
LEAD(): This function provides access to a following row's value within the same partition. SELECT employee_name, salary, LEAD(salary) OVER (ORDER BY hire_date) AS next_salary FROM employees; The LEAD() function retrieves the next row's salary for each employee based on the order of their hire date.
These are just a few examples of SQL window functions. There are many other functions available, such as SUM(), AVG(), MIN(), MAX(), and more, which can be combined with window functions to perform advanced calculations on specific subsets of data within a result set.
why we require having clause with aggregation in SQL?
The HAVING clause in SQL is used in conjunction with the GROUP BY clause to filter the results of an aggregation. While the WHERE clause is used to filter rows before they are grouped and aggregated, the HAVING clause is used to filter the groups after the aggregation has taken place.
Here are a few reasons why the HAVING clause is necessary when working with aggregations in SQL:
Filtering grouped data: When you use the GROUP BY clause to group rows based on certain criteria, the resulting groups may have different aggregate values. The HAVING clause allows you to specify conditions to filter these groups based on their aggregated values. It acts as a filter for groups, retaining only those groups that meet the specified criteria.
Aggregation-specific filtering: The HAVING clause can include conditions that involve aggregate functions, such as SUM(), COUNT(), AVG(), etc. This allows you to filter groups based on the result of these aggregate functions. For example, you can use the HAVING clause to filter groups where the sum of sales exceeds a certain threshold.
Post-aggregation filtering: The HAVING clause operates on the results of the aggregation, allowing you to filter based on the calculated values. This is useful when you want to filter based on aggregated values that cannot be determined before the grouping and aggregation process.
Multiple conditions: The HAVING clause supports complex conditions using logical operators like AND, OR, and NOT. This allows you to specify multiple conditions for filtering grouped data based on different criteria.
Here's an example to illustrate the usage of the HAVING clause:
SQL
SELECT category, AVG(price) AS avg_price
FROM products
GROUP BY category
HAVING AVG(price) > 1000;
In this example, the HAVING clause is used to filter groups where the average price is greater than 1000. Only the groups that meet this condition will be included in the result.
In summary, the HAVING clause is necessary when working with aggregations in SQL because it allows you to filter groups based on their aggregated values, providing a way to further refine your query results.
Few common SQL interview questions that cover various aspects of SQL:
What is SQL? What are its main components?
What are the different types of SQL statements?
Explain the difference between SQL and NoSQL databases.
What is a primary key and foreign key? How are they related?
What is the difference between INNER JOIN and OUTER JOIN? Provide examples.
Explain the GROUP BY clause and its purpose.
What is the difference between WHERE and HAVING clauses?
What is normalization in database design? Why is it important?
Describe the ACID properties in the context of database transactions.
Explain the difference between UNION and UNION ALL operators.
How do you handle NULL values in SQL?
What is a subquery? How is it different from a regular query?
How do you optimize SQL queries for performance?
What are indexes in databases? How do they improve query performance?
What is the purpose of the COMMIT and ROLLBACK statements?
Explain the concept of database transactions.
How can you prevent SQL injection attacks in your code?
What is a correlated subquery? Provide an example.
What is the difference between a clustered index and a non-clustered index?
How would you handle duplicate records in a table?
Explain the difference between CHAR and VARCHAR data types.
What is the purpose of the ORDER BY clause? Can you use it with aggregate functions?
What is the difference between a view and a table?
Explain the concept of database normalization forms.
How do you backup and restore a database?
What is the purpose of the EXISTS operator in SQL?
Describe the difference between a database and a schema.
How do you perform transactions in SQL?
Explain the concept of self-join in SQL.
What are triggers in SQL? How do they work?
These questions cover a range of SQL concepts and are commonly asked during SQL interviews. It's always a good idea to study and practice SQL thoroughly to feel confident in answering these questions effectively.