SQL - Sub Queries
SQL - Sub QueriesA Subquery or Inner query or Nested query is a query within another SQL query and embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.
Let’s break down what happens:
SELECT sub.* FROM ( SELECT * FROM tutorial.sf_crime_incidents_2014_01 WHERE day_of_week = 'Friday' ) sub WHERE sub.resolution = 'NONE'
First, the database runs the “inner query” — the part between the parentheses:
Read more: SQL - Sub Queries
SELECT * FROM tutorial.sf_crime_incidents_2014_01 WHERE day_of_week = 'Friday'