SlideShare a Scribd company logo
1 of 4
Mysql Subquries Example and Query Process
Mysql Subquery
Explanation :
Subquery or Inner query or Nested query is a query in a query.mysql subquery is usually added
in the WHERE Clause of the mysql statement. Most of the time, a subquery is used when you
know how to search for a value using a SELECT statement, but do not know the exact value in
the database.
Subqueries are an alternate way of returning data from multiple tables.
Subqueries can be used with the following SQL statements along with the comparision
operators like
Syntax:
SELECT column_list FROM table-name
[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];
Real Time Query Car ShowRoom Example
DROP TABLE IF EXISTS `cars`;
CREATE TABLE `cars` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(450) NOT NULL,
`cost` double NOT NULL,
PRIMARY KEY (`id`)
) ;
INSERT INTO `cars` (`id`,`name`,`cost`) VALUES
(1,'Audi',52642),
(2,'Mercedes',57127),
(3,'Skoda',9000),
(4,'Volvo',29000),
(5,'Bentley',350000),
(6,'Citroen',21000),
(7,'Hummer',41400),
(8,'Volkswagen ',21600);
DROP TABLE IF EXISTS `cus_table`;
CREATE TABLE `cus_table` (
`customer_id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(450) NOT NULL,
PRIMARY KEY USING BTREE (`customer_id`)
) ;
INSERT INTO `cus_table` (`customer_id`,`name`) VALUES
(1,'Paul Novak'),
(2,'Terry Neils'),
(3,'Jack Fonda'),
(4,'Tom Willis');
DROP TABLE IF EXISTS `reservations`;
CREATE TABLE `reservations` (
`id` int(10) unsigned NOT NULL auto_increment,
`customer_id` varchar(45) NOT NULL,
`Day` datetime NOT NULL,
PRIMARY KEY (`id`)
) ;
INSERT INTO `reservations` (`id`,`customer_id`,`Day`) VALUES
(1,'1','2017-11-22 00:00:00'),
(2,'2','2017-11-22 00:00:00'),
(3,'3','2017-11-22 00:00:00'),
(4,'4','2017-11-22 00:00:00');
Scalar subqueries
SELECT name FROM cus_table WHERE
customer_id=(SELECT customer_id FROM reservations WHERE id=4);
OUTPUT :
Table subqueries
SELECT name FROM cus_table WHERE customer_id IN
(SELECT DISTINCT customer_id FROM reservations);
SELECT DISTINCT name FROM cus_table JOIN reservations
ON cus_table.customer_id=reservations.customer_id;
OUTPUT :
Correlated subqueries
SELECT name FROM cars WHERE cost <
(SELECT AVG(cost) FROM cars);
SELECT name FROM cus_table WHERE NOT EXISTS
(SELECT * FROM reservations WHERE
cus_table.customer_id=reservations.customer_id);
OUTPUT :

More Related Content

Similar to Mysql joins example table and query process

Similar to Mysql joins example table and query process (20)

MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Android sq lite-chapter 22
Android sq lite-chapter 22Android sq lite-chapter 22
Android sq lite-chapter 22
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
 
QB Into the Box 2018
QB Into the Box 2018QB Into the Box 2018
QB Into the Box 2018
 
Dropping unique constraints in sql server
Dropping unique constraints in sql serverDropping unique constraints in sql server
Dropping unique constraints in sql server
 
Sql12
Sql12Sql12
Sql12
 
Using Mysql.pptx
Using Mysql.pptxUsing Mysql.pptx
Using Mysql.pptx
 
MAD UNIT 5 FINAL.pptx
MAD UNIT 5 FINAL.pptxMAD UNIT 5 FINAL.pptx
MAD UNIT 5 FINAL.pptx
 
MySql:Introduction
MySql:IntroductionMySql:Introduction
MySql:Introduction
 
MySQL Introduction
MySQL IntroductionMySQL Introduction
MySQL Introduction
 
MySQL Basics
MySQL BasicsMySQL Basics
MySQL Basics
 
MySql:Basics
MySql:BasicsMySql:Basics
MySql:Basics
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorial
 
MS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmxMS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmx
 
MS SQL SERVER: Data mining concepts and dmx
MS SQL SERVER: Data mining concepts and dmxMS SQL SERVER: Data mining concepts and dmx
MS SQL SERVER: Data mining concepts and dmx
 
PerlApp2Postgresql (2)
PerlApp2Postgresql (2)PerlApp2Postgresql (2)
PerlApp2Postgresql (2)
 
DBIC 2 - Resultsets
DBIC 2 - ResultsetsDBIC 2 - Resultsets
DBIC 2 - Resultsets
 
Les13
Les13Les13
Les13
 
sql.pptx
sql.pptxsql.pptx
sql.pptx
 
A Brief Introduction About Sql Injection in PHP and MYSQL
A Brief Introduction About Sql Injection in PHP and MYSQLA Brief Introduction About Sql Injection in PHP and MYSQL
A Brief Introduction About Sql Injection in PHP and MYSQL
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Recently uploaded (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

Mysql joins example table and query process

  • 1. Mysql Subquries Example and Query Process Mysql Subquery Explanation : Subquery or Inner query or Nested query is a query in a query.mysql subquery is usually added in the WHERE Clause of the mysql statement. Most of the time, a subquery is used when you know how to search for a value using a SELECT statement, but do not know the exact value in the database. Subqueries are an alternate way of returning data from multiple tables. Subqueries can be used with the following SQL statements along with the comparision operators like Syntax: SELECT column_list FROM table-name [WHERE Clause] [GROUP BY clause] [HAVING clause] [ORDER BY clause]; Real Time Query Car ShowRoom Example DROP TABLE IF EXISTS `cars`; CREATE TABLE `cars` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(450) NOT NULL,
  • 2. `cost` double NOT NULL, PRIMARY KEY (`id`) ) ; INSERT INTO `cars` (`id`,`name`,`cost`) VALUES (1,'Audi',52642), (2,'Mercedes',57127), (3,'Skoda',9000), (4,'Volvo',29000), (5,'Bentley',350000), (6,'Citroen',21000), (7,'Hummer',41400), (8,'Volkswagen ',21600); DROP TABLE IF EXISTS `cus_table`; CREATE TABLE `cus_table` ( `customer_id` int(10) unsigned NOT NULL auto_increment, `name` varchar(450) NOT NULL, PRIMARY KEY USING BTREE (`customer_id`) ) ; INSERT INTO `cus_table` (`customer_id`,`name`) VALUES (1,'Paul Novak'), (2,'Terry Neils'), (3,'Jack Fonda'),
  • 3. (4,'Tom Willis'); DROP TABLE IF EXISTS `reservations`; CREATE TABLE `reservations` ( `id` int(10) unsigned NOT NULL auto_increment, `customer_id` varchar(45) NOT NULL, `Day` datetime NOT NULL, PRIMARY KEY (`id`) ) ; INSERT INTO `reservations` (`id`,`customer_id`,`Day`) VALUES (1,'1','2017-11-22 00:00:00'), (2,'2','2017-11-22 00:00:00'), (3,'3','2017-11-22 00:00:00'), (4,'4','2017-11-22 00:00:00'); Scalar subqueries SELECT name FROM cus_table WHERE customer_id=(SELECT customer_id FROM reservations WHERE id=4); OUTPUT :
  • 4. Table subqueries SELECT name FROM cus_table WHERE customer_id IN (SELECT DISTINCT customer_id FROM reservations); SELECT DISTINCT name FROM cus_table JOIN reservations ON cus_table.customer_id=reservations.customer_id; OUTPUT : Correlated subqueries SELECT name FROM cars WHERE cost < (SELECT AVG(cost) FROM cars); SELECT name FROM cus_table WHERE NOT EXISTS (SELECT * FROM reservations WHERE cus_table.customer_id=reservations.customer_id); OUTPUT :