MySQL Tutorial: Subquery Example Detailed Explanation

MySQL Tutorial: Subquery Example Detailed Explanation

1. What is a subquery?

When one query is part of another query, we call the inner query a subquery and the outer query the main query.

2. Where can subqueries appear?

   select
       ..(select).
   from
       ..(select).
   where
       ..(select).

As can be seen from the above, subqueries often appear in three places: select can be followed by a subquery; from can be followed by a subquery; where can be followed by a subquery; we refer to them here as: select子查詢, from子查詢, where子查詢.

3. Where subquery

insert image description here

Exercise: Find information about employees who earn above the average salary.

insert image description here

4. Use a subquery after from. (Too important)

insert image description here

Exercise 1: Find the salary grade for the average salary in each department.

insert image description here

The results are as follows:

insert image description here

Exercise 2: Find the average salary level for each employee

insert image description here

The results are as follows:

insert image description here

Note: There are few scenarios where subqueries are used after select, so they will not be described here. If you feel it is necessary, you can go and check it out yourself.

The above is the detailed content of the MySQL tutorial subquery example. For more information about MySQL subqueries, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Mysql multi-layer subquery example code (Favorites case)
  • In-depth analysis of MySQL subquery principle
  • Solve the use of Mysql multi-row subquery and null value problems
  • mysql subquery and join table details
  • Problems with join queries and subqueries in MySQL
  • Basic use of subqueries in MySQL
  • MySQL subqueries and grouped queries
  • Detailed example of MySQL subquery
  • MySQL detailed analysis of the use of subqueries

<<:  Simple tips to increase web page loading speed

>>:  HTML simple shopping quantity applet

Recommend

Detailed example of sorting function field() in MySQL

Preface In our daily development process, sorting...

Solution to transparent font problem after turning on ClearType in IE

The solution to the transparent font problem after...

Implementation of Vue top tags browsing history

Table of contents nonsense Functions implemented ...

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

Detailed explanation of using javascript to handle common events

Table of contents 1. Form events 2. Mouse events ...

Vue+ElementUI implements paging function-mysql data

Table of contents 1. Problem 2. Solution 2.1 Pagi...

Install Docker for Windows on Windows 10 Home Edition

0. Background Hardware: Xiaomi Notebook Air 13/In...

MySQL deadlock routine: inconsistent batch insertion order under unique index

Preface The essence of deadlock is resource compe...

How to align text boxes in multiple forms in HTML

The form code is as shown in the figure. The styl...

Detailed steps for setting up a nexus server

1. The significance of building nexus service As ...

How to access the local machine (host machine) in Docker

Question How to access the local database in Dock...