当前位置:网站首页>Zero foundation self-study SQL course | sub query

Zero foundation self-study SQL course | sub query

2022-06-24 09:14:00 Meow Ningyi

Hello everyone , I'm Ning Yi .

Today's talk SQL Course No 18 course : Subquery .

SQL Statements can be nested , The most common is the nesting of query statements .

Basic grammar :

SELECT < Field name > 
FROM < Table name > 
WHERE(
  SELECT < Field name > 
  FROM < Table name > 
);

We generally call nested statements outside the main query , The nested statements are sub queries , Sometimes it is called external query 、 Internal query , You know what it means .

Subqueries should be enclosed in brackets . Subqueries can not only be placed in WHERE Behind , It can also be placed in SELECT、FROM Behind , Let's talk about it one by one .

1、 Subquery +WHERE Clause

SQL Execution time , The subquery in parentheses will be executed first , Subqueries are most often associated with WHERE Clause is used in conjunction with . The result of the subquery is as WHERE The filter condition of clause , Complete more complex data retrieval .

example : stay Students In the table , Find out what's going on in " Ning Yi " Students born later .

Instance analysis : You need to make sure " Ning Yi " My birthday , Then take birthday as WHERE filter , Get the final data .

First step : find " Ning Yi " My birthday

SELECT Sage
FROM Students
WHERE Sname = " Ning Yi "

The second step : Take birthday as WHERE filter , Get the final data , Subquery statements should be enclosed in parentheses .

SELECT *
FROM Students
WHERE Sage > (
  SELECT Sage
  FROM Students
  WHERE Sname = " Ning Yi "
)

2、 Subquery + SELECT sentence

Subqueries can also be associated with SELECT Statement combination , The results returned by the subquery , Will be displayed as a column in the result set .

SELECT Subqueries of statements are often used in conjunction with aggregate functions . Because when we use aggregate functions , The record will be combined into one , Other data details cannot be displayed .

such as : We want to see all the student names in the student table 、 Student's birthday 、 Student's biggest birthday .

Sample results :

Wrong writing :

SELECT Sname,Sage,Max(Sage)
FROM Students

It would be wrong to write like this , Because aggregate functions and columns in other tables (Sname,Sage), At the same time SELECT Behind . Need to use GROUP BY Statement to list the columns in these tables (Sname,Sage) grouping .

Add... After the above statement GROUP BY Sname,Sage That's all right. .

But write like this , The data of each group will be aggregated into 1 Data , For example, each group has 3 Data , Using aggregate functions MAX()+GROUP BY, Finally, each group will only show 1 Data of maximum values .

We need to show Students All the students in the table , This is not enough to meet our needs .

Write it correctly : Combined with sub query to realize .

SELECT
  Sname,
  Sage,
  (SELECT Max(Sage) FROM Students) AS Maxage
FROM Students

3、 Subquery +FROM Clause

Subquery and FROM Clause is used in conjunction with , The subquery result is treated as a “ surface ”, It can be used SELECT Statement for further screening .

such as : Let's write one first SELECT Query statement

SELECT
    Sid,
    'student' AS status
FROM Students
WHERE Sid <= 5

Put the above query statement in FROM Behind , Then the result of the above query , Will be regarded as a “ surface ”.

SELECT Sid,status
FROM (
  SELECT
    Sid,
    'student' AS status
  FROM Students
  WHERE Sid <= 5
) AS s --  Alias is required 
WHERE Sid > 2

Here's one thing to pay special attention to , Put it in FROM The following subquery , Must be aliased .

Complex subqueries are nested into FROM It makes the whole query look too complicated , We usually save the sub query results as views , Then directly use the view as the source table , View meeting SQL In the advanced course, we will explain in detail .

In fact, subquery is query statement nesting , There's nothing new , Just one more level , From the inside out, it will be clear .

Homework : combination Students surface , from Teachers Find the teacher who is the head teacher in the table ( Through sub query ).

Homework analysis : First from Students In the table , Find out the names of all the class teachers Tid And remove the weight , Use query results as filter criteria , Put it in WHERE In the sentence .

SELECT *
FROM Teachers
WHERE Tid IN (
  SELECT
    DISTINCT Tid
  FROM Students
)

Click on Focus on , Update the course and notify the first time ~

原网站

版权声明
本文为[Meow Ningyi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240758252178.html