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 explanation of JavaScript implementation of hash table

Table of contents 1. Hash table principle 2. The ...

Website User Experience Design (UE)

I just saw a post titled "Flow Theory and Des...

Introduction to HTML link anchor tags and their role in SEO

The <a> tag is mainly used to define links ...

JavaScript Canvas draws dynamic wireframe effect

This article shares the specific code of JavaScri...

Summary of MySQL InnoDB locks

Table of contents 1. Shared and Exclusive Locks 2...

Negative distance (empathy) - iterative process of mutual influence

Negative distance refers to empathy. Preface (rai...

HTML page common style (recommended)

As shown below: XML/HTML CodeCopy content to clip...

Front-end vue+express file upload and download example

Create a new server.js yarn init -y yarn add expr...

Example code for implementing background transparency and opaque text with CSS3

Recently, I encountered a requirement to display ...

Common methods and problems of Docker cleaning

If you use docker for large-scale development but...

MySQL Series 12 Backup and Recovery

Table of contents Tutorial Series 1. Backup strat...

How to change the terminal to a beautiful command line prompt in Ubuntu 18

I reinstalled VMware and Ubuntu, but the command ...

HTML/CSS Basics - Several precautions in HTML code writing (must read)

The warning points in this article have nothing t...