DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Use a Subquery in MySQL

How to Use a Subquery in MySQL

Let's take a look at a tutorial that explains how to use a subquery in MySQL and also view an example on how to find salaries.

Chandar Bhushan user avatar by
Chandar Bhushan
·
Aug. 28, 18 · Tutorial
Like (2)
Save
Tweet
Share
10.67K Views

Join the DZone community and get the full member experience.

Join For Free

How to Use Subquery in MySQL 

In this tutorial, we will show you how to use the subquery in MySQL.

What is the Subquery? A MySQL subquery is a query nested inside the other query like Select, Delete,  Update and Insert.  Suppose we have the database table as something like below:

ID Fname Lname Email Salary
1

Chandar

bhushan pbchandan3@gmail.com 8000
2 Pavan yadan pavan@gmail.com 7000
3 Gopal thakur goapla@gmail.com 6000
4 Sukhi saini sukhi@gmail.com 5000
5 test last tast@gmail.com 4000
6 new last new@gmail.com 3000

Let's start to understand how the subquery works in MySQL.

As per the above table structure, I just want to get the 2nd and the nth highest salary of the employee.

Step: 1

select max(salary) from Employee;

If you run the above query, you will get a result that looks something like 8,000. It's fine that we get the highest salary from the database, but what if you want to get the 2nd highest salary from the database? for the 2nd highest salary query, let's move on to the 2nd step. 

Step: 2

select max(salary) from Employee where salary < ( select max(salary) from Employee );

When you try to execute the above query, you will get the 2nd highest salary from the database i.e 7,000.  Now we are going to get the nth salary from the database. Nth means 3rd, 4th, 5th, etc.

Step: 3

select DISTINCT salary from Employee where salary < ( select max(salary) from Employee ) ORDER BY Salary DESC LIMIT 1 OFFSET 3;

The above query will return the 6000. It returns 6000 because we use the offset 3, which is the same as if you want to get the 4 and 5. Just replace the 3 with 4 and 5. We can also use the other easy way to get the highest salary from the database because as you know, in this article, we are explaining the subquery, so that’s why I designed the query. Let's try the other way to get the highest and the nth highest salary.

For more details, please go here. 

MySQL Database

Published at DZone with permission of Chandar Bhushan. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Use Java Event Listeners in Selenium WebDriver
  • Microservices 101: Transactional Outbox and Inbox
  • Public Key and Private Key Pairs: Know the Technical Difference
  • Unlock the Power of Terragrunt’s Hierarchy

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: