Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/06. Databases and SQL for Data Science with Python/03. Intermediate SQL/03. Sub-Queries and Nested SELECTS/README.md
Views: 4606
Sub-Queries and Nested SELECTS
Sub-queries or sub selects are like regular queries but placed within parentheses and nested inside another query. This allows you to form more powerful queries than would have been otherwise possible. An example of a nested query is shown. In this example, the sub-query is inside the where clause of another query.
Sub-Select Expressions
Sub-queries are often necessary because aggregate functions like AVG()
cannot be evaluated within a WHERE
clause. E.g. tje following query will result in an error:
But using a sub-select expression will solve the issue, as shown below.
Column Expressions
The sub-select doesn't just have to go in the WHERE
clause. It can also go in other parts of the query, such as in the list of columns to be selected. Such sub-queries are called column expressions.
Say we wanted to compare the salary of each employee with the average salary. The following expression would result in an error:
We can circumvent this error by using the average function in a sub-query placed in the list of the columns. For example,
Derived Tables or Table Expressions
Another option is to make the sub-query be part of the FROM
clause. Sub-queries like these are sometimes called derived tables or table expressions, because the outer query uses the results of the sub-query as a data source, i.e. we substitute the TABLE name with a sub-query. For example,
The derived table in a sub-query does not include sensitive fields like date of birth or salary. This example is a trivial one, and we could just as easily have included the columns in the outer query. However, such derived tables can prove to be powerful in more complex situations such as when working with multiple tables and doing joins.
Nested SELECT
Statements
A Typical Nested SELECT
Statement looks like: