Four ways to introduce a subquery
in a SELECT statement
1. In a WHERE clause as a search condition
2. In a HAVING clause as a search condition
3. In the FROM clause as a table specification
4. In the SELECT clause as a column specification
NOTE: A query with a join typically performs faster than the same query
with a subquerySELECT column_name
 FROM table_name1
WHERE VALUE IN 
(SELECT column_name
FROM table_name2 
WHERE condition)
USE AdventureWorks2016;
GO
SELECT Ord.SalesOrderID, Ord.OrderDate,
    (SELECT MAX(OrdDet.UnitPrice)
     FROM Sales.SalesOrderDetail AS OrdDet
     WHERE Ord.SalesOrderID = OrdDet.SalesOrderID) AS MaxUnitPrice
FROM Sales.SalesOrderHeader AS Ord;
GO-- Simple query with two tables
SELECT left_table.colx, right_table.coly
FROM left_table, right_table
WHERE left_table.common_col = right_table.common_col

-- Subquery inside FROM (turning a query into virtual table using alias, evolving from the previous query)
SELECT left_table.colx, right_table.coly
FROM left_table,
    (SELECT coly, colz, common_col          -- subquery start
    FROM another_table) AS right_table      -- subquery end
WHERE left_table.common_col = right_table.common_col
ORDER BY continent;


-- Subquery inside WHERE (You use it in Semi join / Anti Join)
SELECT left_table.*
FROM left_table
WHERE left_table.some_col <NOT> IN  -- Using "NOT" will result in Anti join
        (                           -- subquery start
            SELECT another_col      
            FROM right_table
        );                          -- subquery end

-- Subquery inside SELECT (REMEMBER: This query should only produce a single value)
SELECT
    outer_table.id,
    (SELECT COUNT(*) FROM inner_table WHERE outer_table.id = inner_table.id) AS new_col_1,              -- subquery 1
    (SELECT AVG(some_column) FROM inner_table WHERE outer_table.id = inner_table.id) AS new_col_2,      -- subquery 2
    (SELECT MAX(another_column) FROM inner_table WHERE outer_table.id = inner_table.id) AS new_col_3    -- subquery 3
FROM
    outer_table;

-- Using the WITH keyword (creating multiple virtual tables)
WITH
result_table1 AS (
    SELECT col1, col2 FROM t1),             -- subquery 1
result_table2 AS (
    SELECT colx, coly FROM t2)              -- subquery 2
SELECT * FROM result_table1, result_table2
WHERE result_table1.col1 <= result_table2.col2SELECT *
FROM Customers
WHERE age = (
  SELECT MIN(age)
  FROM Customers
);SUBQUERY IS NESTED QUERY INSIDE A SELECT, 
INSERT OR UPDATE METHODS. OR INSIDE ANOTHER SUBQUERY
SELECT name, listed_price
FROM paintings
WHERE listed_price > (
    SELECT AVG(listed_price)
    FROM paintings
);UPDATE Book SET note= 'editore: ' + (SELECT name FROM Publisher WHERE Publisher.ID = Book.publisher)-- Example 1: Using a subquery in the WHERE clause

SELECT column_name
FROM table_name
WHERE column_name IN (SELECT column_name FROM another_table);

-- Example 2: Using a subquery in the FROM clause

SELECT t1.column_name
FROM (SELECT column_name FROM table_name WHERE condition) AS t1
JOIN table_name2 AS t2 ON t1.column_name = t2.column_name;

-- Example 3: Using a subquery in the SELECT clause

SELECT column_name, (SELECT MAX(value) FROM another_table) AS max_value
FROM table_name;UPDATE dataflair_emp1 
SET salary=35000
WHERE emp_id  = ( SELECT emp_id
                    FROM dataflair_emp1
                    WHERE post='Sr.Manager');     
select * from dataflair_emp1;
SELECT incidents.*, sub.incidents AS incidents_that_day
FROM tutorial.sf_crime_incidents_2014_01 incidents
JOIN (
   SELECT date, COUNT(incidnt_num) AS incidents
   FROM tutorial.sf_crime_incidents_2014_01
   GROUP BY 1
) sub ON incidents.date = sub.date
ORDER BY sub.incidents DESC, time;

SQL相关代码片段

spring postgresql example

test existence of an SQL table before deleting

sql student form

mysql database structure export

postgresql insert datetime example

relational dbms

brew mysql

is operator sql

What is Partitioned_By in SQL?

for loop pl/sql

oracle change column type

convert sql table to c# class

mysql date time month

SQL create table

show store procedure create process

MISSIng index sql server

postgres update column

pagination in mssql

how to search in myanmar unicode in ssms database

how to get first and last day of month in sql

oracle list foreign keys referencing a table

what is unsigned in mysql

Impala Wildcard Search Query

sql server force parallelism

oracle list of materialized views

ajouter un mot de passer mysql

show view code mssql

Filter window function

oracle get plan_hash_value from "explain plan"

Incorrect syntax near 'RSA_512'.

analyze table oracle

mysql created_at and updated_at add value

supabase "permission denied for schema public"

get age using sql query

phpmyadmin get list of all databases with sizes

how to get scend number row from mysql database

create a sequence in oracle sql

does not contain KQL (Kusto)

how to alter data enum status in postgres

how to alter data enum status

order by sql multiple columns with case

idle connections

create table using existing table bigquery

perform sql inset comand in c#

oracle apex avoid confirmation

mysql ERROR: ASCII '\0' appeared in the statement

oracle date to timestamp with timezone

check what is using sql cpu on sql server

wp secure sql query

list out custom functions cmd in mysql

sql having count is null

find particular column or table name from whole db

EPOCH postgres

MSSQL Work table

float to varchar in sql

sqlplus generate awr

length vs char_length mysql

get last inserted primary key mysqli

alter multiple columns in table sql server

psql check

editeur plSQL en ligne

sql get last 2 month

levenshtein sql mysql 8

CONVERT Date Formats

postgresql root password change

postgresql root password reset

is null and is not null in sql

mysql cli create database with engine and charset

dateformat mysql

creating a tablespace

mysql connection refused docker

reset table mysql

oracle plsql how to print array type

hive insert from select

sql delete all the database tables

sql create command

sql server select if else example

how to make the dump out of database in postgresql

how to save sql_mode in mysql permanently

created_at updated_at mysql