Docsity

SQL Subqueries Notes for Beginners, Thesis of Computer Science

Partial preview of the text

Download SQL Subqueries Notes for Beginners and more Thesis Computer Science in PDF only on Docsity!

๐Ÿ“‘ Contents

1. Introduction to SQL Subqueries

2. Types of Subqueries

3. Subquery in WHERE Clause

4. Subquery in SELECT Clause

5. Correlated Subqueries

6. Subquery vs JOIN

7. Real-World Applications of Subqueries

8. Common Mistakes in Subqueries

9. Conclusion

SQL SUBQUERIES

๐Ÿง  1. Introduction to SQL Subqueries

A SQL subquery is a query written inside another query. It is also known as a nested query or inner query. Subqueries are used when a query depends on the result of another query. Instead of writing multiple separate queries and manually combining results, subqueries allow SQL to handle this logic internally in a structured and efficient way. In real-world scenarios, data often needs to be filtered based on dynamic conditions. For example, instead of manually finding the average salary and then writing another query, a subquery allows this to be done in one step. This makes queries more powerful and flexible. Subqueries can be used in SELECT, WHERE, FROM, and HAVING clauses. This flexibility allows them to solve complex problems such as ranking, filtering, and conditional comparisons. They are especially useful when working with large datasets where intermediate results are required. A subquery runs first, and its output is then used by the outer query. This execution order is important because the outer query depends on the result of the inner query. Understanding subqueries is essential for advanced SQL because they are widely used in analytics, reporting, and real-world database systems.

passed to the outer query. Understanding these types helps in choosing the right approach for solving SQL problems efficiently. ๐Ÿง  3. Subquery in WHERE Clause Subqueries are most commonly used in the WHERE clause. They allow filtering of data based on the result of another query. Example: SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees); In this query, the subquery calculates the average salary of all employees. The outer query then selects employees whose salary is greater than this average. This approach is powerful because it eliminates the need for manual calculations. The database handles everything in one query. Subqueries in WHERE clause are useful for comparisons, filtering, and conditional logic. They are commonly used in performance analysis, ranking systems, and reporting tasks.

๐Ÿ“Š 4. Subquery in SELECT Clause

Subqueries can also be used in the SELECT clause to create calculated columns. Example: SELECT name, (SELECT AVG(salary) FROM employees) AS avg_salary FROM employees; Here, the subquery calculates the average salary and displays it alongside each row. This allows comparison between individual values and overall statistics. This type of subquery is useful in dashboards and reports where contextual information is required.

๐Ÿ”„ 5. Correlated Subquery A correlated subquery depends on the outer query and executes once for each row. Example: SELECT name FROM employees e WHERE salary > ( SELECT AVG(salary) FROM employees e WHERE e1.department = e2.department ); This query finds employees who earn more than the average salary of their department. Correlated subqueries are powerful but slower because they run multiple times. They should be used carefully in large datasets. โš–๏ธ 6. Subquery vs JOIN

Subqueries and joins are both used to combine data, but they work differently.Subqueries are easier to read and useful for nested logic. Joins are generally faster and more efficient for large datasets.