
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we use a MySQL subquery with FROM clause?
Subqueries can work well in a SELECT statement FROM clause. Following is the syntax for the same −
SELECT … FROM(subquery) [AS] name …
To make it understand we are using the following data from table ‘cars’ −
mysql> Select * from Cars; +------+--------------+---------+ | ID | Name | Price | +------+--------------+---------+ | 1 | Nexa | 750000 | | 2 | Maruti Swift | 450000 | | 3 | BMW | 4450000 | | 4 | VOLVO | 2250000 | | 5 | Alto | 250000 | | 6 | Skoda | 1250000 | | 7 | Toyota | 2400000 | | 8 | Ford | 1100000 | +------+--------------+---------+ 8 rows in set (0.02 sec)
Now in the following query, we are using the subquery with FROM clause −
mysql> Select Id_New, Name_New, Price_New FROM (SELECT Id as Id_New, Name as Name_New, Price as Price_New FROM Cars) as c Where Price_New > 500000; +--------+----------+-----------+ | Id_New | Name_New | Price_New | +--------+----------+-----------+ | 1 | Nexa | 750000 | | 3 | BMW | 4450000 | | 4 | VOLVO | 2250000 | | 6 | Skoda | 1250000 | | 7 | Toyota | 2400000 | | 8 | Ford | 1100000 | +--------+----------+-----------+ 6 rows in set (0.00 sec)
- Related Articles
- How can I use MySQL subquery as a table in FROM clause?
- How can we use a MySQL subquery with INSERT statement?
- How can we use MySQL SELECT without FROM clause?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- How can we use ASCII() function with MySQL WHERE clause?
- How can we use CHAR_LENGTH() function with MySQL WHERE clause?
- How can we use BIN() function with MySQL WHERE clause?
- How can we use FIND_IN_SET() function with MySQL WHERE clause?
- How can we use MySQL INSTR() function with WHERE clause?
- How can we use MySQL SUM() function with HAVING clause?
- How can we use two columns with MySQL WHERE clause?
- How can we create a MySQL view with a subquery?
- How can we use WHERE clause with MySQL INSERT INTO command?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- How can we filter data with the help of MySQL subquery?

Advertisements