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

Example code for realizing charging effect of B station with css+svg

difficulty Two mask creation of svg graphics Firs...

Innodb system table space maintenance method

Environmental Description: There is a running MyS...

Vue implements bottom query function

This article example shares the specific code of ...

Vue implements zip file download

This article example shares the specific code of ...

Vue3 + TypeScript Development Summary

Table of contents Vue3 + TypeScript Learning 1. E...

How to use VirtualBox to simulate a Linux cluster

1. Set up HOST on the host Macbook The previous d...

Detailed explanation of the processing of the three Docker Nginx Logs

Because colleagues in the company need Nginx log ...

Detailed analysis of the principles and usage of MySQL views

Preface: In MySQL, views are probably one of the ...

About the problem of dynamic splicing src image address of img in Vue

Let's take a look at the dynamic splicing of ...

WeChat applet selects the image control

This article example shares the specific code for...

How to implement a binary search tree using JavaScript

One of the most commonly used and discussed data ...

JavaScript implements an input box component

This article example shares the specific code for...

Detailed explanation of persistent storage of redis under docker

In this chapter, we will start to operate redis i...