SlideShare a Scribd company logo
2
Most read
4
Most read
5
Most read
Practical No: 4
Problem Statement: Design at least 10 SQL queries for suitable database
application using SQL DML statements:
all types of Join, Sub-Query and View.
--------------------------------------------------------------------------------
-------------------------------
##Sub-Query
mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno= (select deptno from emp
where ename="SMITH");
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| SMITH | 800.00 | 20 |
| JONES | 2975.00 | 20 |
| SCOTT | 3000.00 | 20 |
| ADAMS | 1100.00 | 20 |
| FORD | 3000.00 | 20 |
+-------+---------+--------+
5 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno= (select deptno from emp
where ename="SMITH") and ename<>"SMITH";
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| JONES | 2975.00 | 20 |
| SCOTT | 3000.00 | 20 |
| ADAMS | 1100.00 | 20 |
| FORD | 3000.00 | 20 |
+-------+---------+--------+
4 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno= (select deptno from emp
where ename="SMITH");
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| SMITH | 800.00 | 20 |
| JONES | 2975.00 | 20 |
| SCOTT | 3000.00 | 20 |
| ADAMS | 1100.00 | 20 |
| FORD | 3000.00 | 20 |
+-------+---------+--------+
5 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where sal> (select sal from emp where
ename="SMITH");
+--------+---------+--------+
| ename | sal | deptno |
+--------+---------+--------+
| ALLEN | 1600.00 | 30 |
| WARD | 1250.00 | 30 |
| JONES | 2975.00 | 20 |
| MARTIN | 1250.00 | 30 |
| BLAKE | 2850.00 | 30 |
| CLARK | 2450.00 | 10 |
| SCOTT | 3000.00 | 20 |
| KING | 5000.00 | 10 |
| TURNER | 1500.00 | 30 |
| ADAMS | 1100.00 | 20 |
| JAMES | 950.00 | 30 |
| FORD | 3000.00 | 20 |
| MILLER | 1300.00 | 10 |
+--------+---------+--------+
13 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where sal> (select sal from emp where
ename="SMITH") and ename<>"SMITH";
+--------+---------+--------+
| ename | sal | deptno |
+--------+---------+--------+
| ALLEN | 1600.00 | 30 |
| WARD | 1250.00 | 30 |
| JONES | 2975.00 | 20 |
| MARTIN | 1250.00 | 30 |
| BLAKE | 2850.00 | 30 |
| CLARK | 2450.00 | 10 |
| SCOTT | 3000.00 | 20 |
| KING | 5000.00 | 10 |
| TURNER | 1500.00 | 30 |
| ADAMS | 1100.00 | 20 |
| JAMES | 950.00 | 30 |
| FORD | 3000.00 | 20 |
| MILLER | 1300.00 | 10 |
+--------+---------+--------+
13 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where sal in(select sal from emp where
ename="SMITH" OR ename="SCOTT" );
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| SMITH | 800.00 | 20 |
| SCOTT | 3000.00 | 20 |
| FORD | 3000.00 | 20 |
+-------+---------+--------+
3 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where sal in(select sal from emp where
ename="SMITH" OR ename="SCOTT" );
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| SMITH | 800.00 | 20 |
| SCOTT | 3000.00 | 20 |
| FORD | 3000.00 | 20 |
+-------+---------+--------+
3 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno in(select deptno from emp
where ename="SMITH" OR ename="SCOTT" );
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| SMITH | 800.00 | 20 |
| JONES | 2975.00 | 20 |
| SCOTT | 3000.00 | 20 |
| ADAMS | 1100.00 | 20 |
| FORD | 3000.00 | 20 |
+-------+---------+--------+
5 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno in(select deptno from emp
where ename="SMITH" OR ename="MILLER" );
+--------+---------+--------+
| ename | sal | deptno |
+--------+---------+--------+
| SMITH | 800.00 | 20 |
| JONES | 2975.00 | 20 |
| CLARK | 2450.00 | 10 |
| SCOTT | 3000.00 | 20 |
| KING | 5000.00 | 10 |
| ADAMS | 1100.00 | 20 |
| FORD | 3000.00 | 20 |
| MILLER | 1300.00 | 10 |
+--------+---------+--------+
8 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno > ALL(select deptno from
emp where ename="SMITH" OR ename="MILLER" );
+--------+---------+--------+
| ename | sal | deptno |
+--------+---------+--------+
| ALLEN | 1600.00 | 30 |
| WARD | 1250.00 | 30 |
| MARTIN | 1250.00 | 30 |
| BLAKE | 2850.00 | 30 |
| TURNER | 1500.00 | 30 |
| JAMES | 950.00 | 30 |
+--------+---------+--------+
6 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno > ANY(select deptno from
emp where ename="SMITH" OR ename="MILLER" );
+--------+---------+--------+
| ename | sal | deptno |
+--------+---------+--------+
| SMITH | 800.00 | 20 |
| ALLEN | 1600.00 | 30 |
| WARD | 1250.00 | 30 |
| JONES | 2975.00 | 20 |
| MARTIN | 1250.00 | 30 |
| BLAKE | 2850.00 | 30 |
| SCOTT | 3000.00 | 20 |
| TURNER | 1500.00 | 30 |
| ADAMS | 1100.00 | 20 |
| JAMES | 950.00 | 30 |
| FORD | 3000.00 | 20 |
+--------+---------+--------+
11 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno > ANY(select deptno from
emp where ename="TURNER" OR ename="MILLER" );
+--------+---------+--------+
| ename | sal | deptno |
+--------+---------+--------+
| SMITH | 800.00 | 20 |
| ALLEN | 1600.00 | 30 |
| WARD | 1250.00 | 30 |
| JONES | 2975.00 | 20 |
| MARTIN | 1250.00 | 30 |
| BLAKE | 2850.00 | 30 |
| SCOTT | 3000.00 | 20 |
| TURNER | 1500.00 | 30 |
| ADAMS | 1100.00 | 20 |
| JAMES | 950.00 | 30 |
| FORD | 3000.00 | 20 |
+--------+---------+--------+
11 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno > ANY(select deptno from
emp where ename="TURNER" OR ename="SCOTT" );
+--------+---------+--------+
| ename | sal | deptno |
+--------+---------+--------+
| ALLEN | 1600.00 | 30 |
| WARD | 1250.00 | 30 |
| MARTIN | 1250.00 | 30 |
| BLAKE | 2850.00 | 30 |
| TURNER | 1500.00 | 30 |
| JAMES | 950.00 | 30 |
+--------+---------+--------+
6 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where deptno > all(select deptno from
emp where ename="TURNER" OR ename="SCOTT" );
Empty set (0.00 sec)
mysql> select ename,sal,deptno from emp where sal > ANY(select sal from emp
where ename="TURNER" OR ename="SCOTT" );
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| ALLEN | 1600.00 | 30 |
| JONES | 2975.00 | 20 |
| BLAKE | 2850.00 | 30 |
| CLARK | 2450.00 | 10 |
| SCOTT | 3000.00 | 20 |
| KING | 5000.00 | 10 |
| FORD | 3000.00 | 20 |
+-------+---------+--------+
7 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where sal > ALL(select sal from emp
where ename="TURNER" OR ename="SCOTT" );
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| KING | 5000.00 | 10 |
+-------+---------+--------+
1 row in set (0.00 sec)
mysql> select ename,deptno from emp where deptno=(select deptno from dept where
dname="RESEARCH");
+-------+--------+
| ename | deptno |
+-------+--------+
| SMITH | 20 |
| JONES | 20 |
| SCOTT | 20 |
| ADAMS | 20 |
| FORD | 20 |
+-------+--------+
5 rows in set (0.00 sec)
##Jion
mysql> create table orange(o_id varchar(20),price int(12));
Query OK, 0 rows affected (0.35 sec)
mysql> create table apple(a_id varchar(20),price int(12));
Query OK, 0 rows affected (0.06 sec)
mysql> insert into orange values("O1",50),("O2",60);
Query OK, 2 rows affected (0.30 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into apple values("A1",60),("A2",60);
Query OK, 2 rows affected (0.30 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from apple;
+------+-------+
| a_id | price |
+------+-------+
| A1 | 60 |
| A2 | 60 |
+------+-------+
2 rows in set (0.00 sec)
mysql> select * from orange;
+------+-------+
| o_id | price |
+------+-------+
| O1 | 50 |
| O2 | 60 |
+------+-------+
2 rows in set (0.00 sec)
mysql> select * from orange,apple;
+------+-------+------+-------+
| o_id | price | a_id | price |
+------+-------+------+-------+
| O1 | 50 | A1 | 60 |
| O2 | 60 | A1 | 60 |
| O1 | 50 | A2 | 60 |
| O2 | 60 | A2 | 60 |
+------+-------+------+-------+
4 rows in set (0.00 sec)
mysql> select * from orange o inner join apple a on o.price=a.price;
+------+-------+------+-------+
| o_id | price | a_id | price |
+------+-------+------+-------+
| O2 | 60 | A1 | 60 |
| O2 | 60 | A2 | 60 |
+------+-------+------+-------+
2 rows in set (0.04 sec)
mysql> select * from orange o left outer join apple a on o.price=a.price;
+------+-------+------+-------+
| o_id | price | a_id | price |
+------+-------+------+-------+
| O2 | 60 | A1 | 60 |
| O2 | 60 | A2 | 60 |
| O1 | 50 | NULL | NULL |
+------+-------+------+-------+
3 rows in set (0.00 sec)
mysql> select * from orange o right outer join apple a on o.price=a.price;
+------+-------+------+-------+
| o_id | price | a_id | price |
+------+-------+------+-------+
| O2 | 60 | A1 | 60 |
| O2 | 60 | A2 | 60 |
+------+-------+------+-------+
2 rows in set (0.00 sec)
mysql> (select * from orange o left outer join apple a on o.price=a.price)
union(select * from orange o right outer join apple a on o.price=a.price);
+------+-------+------+-------+
| o_id | price | a_id | price |
+------+-------+------+-------+
| O2 | 60 | A1 | 60 |
| O2 | 60 | A2 | 60 |
| O1 | 50 | NULL | NULL |
+------+-------+------+-------+

More Related Content

PDF
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
bhavesh lande
 
PDF
database application using SQL DML statements: Insert, Select, Update, Delet...
bhavesh lande
 
PDF
Unnamed PL/SQL code block: Use of Control structure and Exception handling i...
bhavesh lande
 
PPT
Java Servlets
BG Java EE Course
 
TXT
Getweeklyhoursbyuser
hasheemm
 
PPTX
Postgresql
NexThoughts Technologies
 
DOC
Sql queries with answers
vijaybusu
 
PPTX
Prim's algorithm
Pankaj Thakur
 
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
bhavesh lande
 
database application using SQL DML statements: Insert, Select, Update, Delet...
bhavesh lande
 
Unnamed PL/SQL code block: Use of Control structure and Exception handling i...
bhavesh lande
 
Java Servlets
BG Java EE Course
 
Getweeklyhoursbyuser
hasheemm
 
Sql queries with answers
vijaybusu
 
Prim's algorithm
Pankaj Thakur
 

What's hot (20)

PDF
Sql queries questions and answers
Michael Belete
 
PPTX
String matching algorithms(knuth morris-pratt)
Neel Shah
 
PPTX
Sub query example with advantage and disadvantages
Sarfaraz Ghanta
 
PPTX
Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...
Mahbubur Rahman
 
DOC
SQL practice questions - set 3
Mohd Tousif
 
PPT
session.ppt
pshtiwanabasabdulla
 
PDF
Web Development with Python and Django
Michael Pirnat
 
PPTX
PostGIS and Spatial SQL
Todd Barr
 
PPTX
Web browser architecture
Nguyen Quang
 
PPTX
Lecture optimal binary search tree
Divya Ks
 
PPT
The Evolution of Java
Fu Cheng
 
PPTX
Android Services
Ahsanul Karim
 
PDF
aggregation and indexing with suitable example using MongoDB.
bhavesh lande
 
PDF
SQL window functions for MySQL
Dag H. Wanvik
 
PDF
PostgreSQL Tutorial For Beginners | Edureka
Edureka!
 
PPT
Constraints In Sql
Anurag
 
PPTX
MySQL 5.7 String Functions
Francesco Marino
 
PDF
Your first ClickHouse data warehouse
Altinity Ltd
 
PPTX
Database Connectivity in PHP
Taha Malampatti
 
DOC
Dbms lab questions
Parthipan Parthi
 
Sql queries questions and answers
Michael Belete
 
String matching algorithms(knuth morris-pratt)
Neel Shah
 
Sub query example with advantage and disadvantages
Sarfaraz Ghanta
 
Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...
Mahbubur Rahman
 
SQL practice questions - set 3
Mohd Tousif
 
session.ppt
pshtiwanabasabdulla
 
Web Development with Python and Django
Michael Pirnat
 
PostGIS and Spatial SQL
Todd Barr
 
Web browser architecture
Nguyen Quang
 
Lecture optimal binary search tree
Divya Ks
 
The Evolution of Java
Fu Cheng
 
Android Services
Ahsanul Karim
 
aggregation and indexing with suitable example using MongoDB.
bhavesh lande
 
SQL window functions for MySQL
Dag H. Wanvik
 
PostgreSQL Tutorial For Beginners | Edureka
Edureka!
 
Constraints In Sql
Anurag
 
MySQL 5.7 String Functions
Francesco Marino
 
Your first ClickHouse data warehouse
Altinity Ltd
 
Database Connectivity in PHP
Taha Malampatti
 
Dbms lab questions
Parthipan Parthi
 
Ad

Similar to database application using SQL DML statements: all types of Join, Sub-Query and View. (20)

DOCX
Mysql and html
Sai Sathvick Chirakala
 
TXT
Empresa completo
Lorraine Cuesta
 
PDF
Adobe Scan 02-Sept-2024.pdf sql programs
sampritikundu65
 
DOCX
Statements project 2
AlexisHarvey8
 
PDF
Sangam 19 - Analytic SQL
Connor McDonald
 
PPTX
Analytic SQL Sep 2013
Connor McDonald
 
TXT
Cat database
tubbeles
 
PPTX
Analytics functions in mysql, oracle and hive
Ankit Beohar
 
PPT
Intro to my sql
MusTufa Nullwala
 
PDF
Mysqlfunctions
N13M
 
PDF
MySQL5.7で遊んでみよう
yoku0825
 
PDF
MySQL Idiosyncrasies That Bite SF
Ronald Bradford
 
PPT
My sql statements by okello erick
okelloerick
 
DOC
80 different SQL Queries with output
Nexus
 
DOC
ORACLE NOTES
Sachin Shukla
 
TXT
Hanya contoh saja dari xampp
Bina Sarana Informatika
 
PDF
MySQL SQL Tutorial
Chien Chung Shen
 
DOCX
Tugas praktikum smbd
Amar Senjaku Ofdetraisar
 
ODP
Explain
Ligaya Turmelle
 
PDF
KScope19 - SQL Features
Connor McDonald
 
Mysql and html
Sai Sathvick Chirakala
 
Empresa completo
Lorraine Cuesta
 
Adobe Scan 02-Sept-2024.pdf sql programs
sampritikundu65
 
Statements project 2
AlexisHarvey8
 
Sangam 19 - Analytic SQL
Connor McDonald
 
Analytic SQL Sep 2013
Connor McDonald
 
Cat database
tubbeles
 
Analytics functions in mysql, oracle and hive
Ankit Beohar
 
Intro to my sql
MusTufa Nullwala
 
Mysqlfunctions
N13M
 
MySQL5.7で遊んでみよう
yoku0825
 
MySQL Idiosyncrasies That Bite SF
Ronald Bradford
 
My sql statements by okello erick
okelloerick
 
80 different SQL Queries with output
Nexus
 
ORACLE NOTES
Sachin Shukla
 
Hanya contoh saja dari xampp
Bina Sarana Informatika
 
MySQL SQL Tutorial
Chien Chung Shen
 
Tugas praktikum smbd
Amar Senjaku Ofdetraisar
 
KScope19 - SQL Features
Connor McDonald
 
Ad

More from bhavesh lande (18)

PDF
The Annual G20 Scorecard – Research Performance 2019
bhavesh lande
 
PDF
information control and Security system
bhavesh lande
 
PDF
information technology and infrastructures choices
bhavesh lande
 
PDF
ethical issues,social issues
bhavesh lande
 
PDF
managing inforamation system
bhavesh lande
 
PDF
• E-commerce, e-business ,e-governance
bhavesh lande
 
PDF
IT and innovations
bhavesh lande
 
PDF
organisations and information systems
bhavesh lande
 
PDF
IT stratergy and digital goods
bhavesh lande
 
PDF
Implement Mapreduce with suitable example using MongoDB.
bhavesh lande
 
PDF
working with python
bhavesh lande
 
PDF
applications and advantages of python
bhavesh lande
 
PDF
introduction of python in data science
bhavesh lande
 
PDF
tools
bhavesh lande
 
PDF
data scientists and their role
bhavesh lande
 
PDF
applications
bhavesh lande
 
PDF
statistics techniques to deal with data
bhavesh lande
 
PPTX
introduction to data science
bhavesh lande
 
The Annual G20 Scorecard – Research Performance 2019
bhavesh lande
 
information control and Security system
bhavesh lande
 
information technology and infrastructures choices
bhavesh lande
 
ethical issues,social issues
bhavesh lande
 
managing inforamation system
bhavesh lande
 
• E-commerce, e-business ,e-governance
bhavesh lande
 
IT and innovations
bhavesh lande
 
organisations and information systems
bhavesh lande
 
IT stratergy and digital goods
bhavesh lande
 
Implement Mapreduce with suitable example using MongoDB.
bhavesh lande
 
working with python
bhavesh lande
 
applications and advantages of python
bhavesh lande
 
introduction of python in data science
bhavesh lande
 
data scientists and their role
bhavesh lande
 
applications
bhavesh lande
 
statistics techniques to deal with data
bhavesh lande
 
introduction to data science
bhavesh lande
 

Recently uploaded (20)

PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PPT
Ppt for engineering students application on field effect
lakshmi.ec
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PPTX
easa module 3 funtamental electronics.pptx
tryanothert7
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
Ppt for engineering students application on field effect
lakshmi.ec
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
easa module 3 funtamental electronics.pptx
tryanothert7
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Information Retrieval and Extraction - Module 7
premSankar19
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 

database application using SQL DML statements: all types of Join, Sub-Query and View.

  • 1. Practical No: 4 Problem Statement: Design at least 10 SQL queries for suitable database application using SQL DML statements: all types of Join, Sub-Query and View. -------------------------------------------------------------------------------- ------------------------------- ##Sub-Query mysql> select * from emp; +-------+--------+-----------+------+------------+---------+---------+--------+ | empno | ename | job | mgr | hiredate | sal | comm | deptno | +-------+--------+-----------+------+------------+---------+---------+--------+ | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 | | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 | | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 | | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 | | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 | | 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 | 3000.00 | NULL | 20 | | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 | | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 | | 7876 | ADAMS | CLERK | 7788 | 1983-01-12 | 1100.00 | NULL | 20 | | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 | | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 | | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 | +-------+--------+-----------+------+------------+---------+---------+--------+ 14 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno= (select deptno from emp where ename="SMITH"); +-------+---------+--------+ | ename | sal | deptno | +-------+---------+--------+ | SMITH | 800.00 | 20 | | JONES | 2975.00 | 20 | | SCOTT | 3000.00 | 20 | | ADAMS | 1100.00 | 20 | | FORD | 3000.00 | 20 | +-------+---------+--------+ 5 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno= (select deptno from emp where ename="SMITH") and ename<>"SMITH"; +-------+---------+--------+ | ename | sal | deptno | +-------+---------+--------+ | JONES | 2975.00 | 20 | | SCOTT | 3000.00 | 20 | | ADAMS | 1100.00 | 20 | | FORD | 3000.00 | 20 | +-------+---------+--------+ 4 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno= (select deptno from emp where ename="SMITH"); +-------+---------+--------+ | ename | sal | deptno | +-------+---------+--------+ | SMITH | 800.00 | 20 | | JONES | 2975.00 | 20 | | SCOTT | 3000.00 | 20 |
  • 2. | ADAMS | 1100.00 | 20 | | FORD | 3000.00 | 20 | +-------+---------+--------+ 5 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where sal> (select sal from emp where ename="SMITH"); +--------+---------+--------+ | ename | sal | deptno | +--------+---------+--------+ | ALLEN | 1600.00 | 30 | | WARD | 1250.00 | 30 | | JONES | 2975.00 | 20 | | MARTIN | 1250.00 | 30 | | BLAKE | 2850.00 | 30 | | CLARK | 2450.00 | 10 | | SCOTT | 3000.00 | 20 | | KING | 5000.00 | 10 | | TURNER | 1500.00 | 30 | | ADAMS | 1100.00 | 20 | | JAMES | 950.00 | 30 | | FORD | 3000.00 | 20 | | MILLER | 1300.00 | 10 | +--------+---------+--------+ 13 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where sal> (select sal from emp where ename="SMITH") and ename<>"SMITH"; +--------+---------+--------+ | ename | sal | deptno | +--------+---------+--------+ | ALLEN | 1600.00 | 30 | | WARD | 1250.00 | 30 | | JONES | 2975.00 | 20 | | MARTIN | 1250.00 | 30 | | BLAKE | 2850.00 | 30 | | CLARK | 2450.00 | 10 | | SCOTT | 3000.00 | 20 | | KING | 5000.00 | 10 | | TURNER | 1500.00 | 30 | | ADAMS | 1100.00 | 20 | | JAMES | 950.00 | 30 | | FORD | 3000.00 | 20 | | MILLER | 1300.00 | 10 | +--------+---------+--------+ 13 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where sal in(select sal from emp where ename="SMITH" OR ename="SCOTT" ); +-------+---------+--------+ | ename | sal | deptno | +-------+---------+--------+ | SMITH | 800.00 | 20 | | SCOTT | 3000.00 | 20 | | FORD | 3000.00 | 20 | +-------+---------+--------+ 3 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where sal in(select sal from emp where ename="SMITH" OR ename="SCOTT" ); +-------+---------+--------+ | ename | sal | deptno | +-------+---------+--------+
  • 3. | SMITH | 800.00 | 20 | | SCOTT | 3000.00 | 20 | | FORD | 3000.00 | 20 | +-------+---------+--------+ 3 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno in(select deptno from emp where ename="SMITH" OR ename="SCOTT" ); +-------+---------+--------+ | ename | sal | deptno | +-------+---------+--------+ | SMITH | 800.00 | 20 | | JONES | 2975.00 | 20 | | SCOTT | 3000.00 | 20 | | ADAMS | 1100.00 | 20 | | FORD | 3000.00 | 20 | +-------+---------+--------+ 5 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno in(select deptno from emp where ename="SMITH" OR ename="MILLER" ); +--------+---------+--------+ | ename | sal | deptno | +--------+---------+--------+ | SMITH | 800.00 | 20 | | JONES | 2975.00 | 20 | | CLARK | 2450.00 | 10 | | SCOTT | 3000.00 | 20 | | KING | 5000.00 | 10 | | ADAMS | 1100.00 | 20 | | FORD | 3000.00 | 20 | | MILLER | 1300.00 | 10 | +--------+---------+--------+ 8 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno > ALL(select deptno from emp where ename="SMITH" OR ename="MILLER" ); +--------+---------+--------+ | ename | sal | deptno | +--------+---------+--------+ | ALLEN | 1600.00 | 30 | | WARD | 1250.00 | 30 | | MARTIN | 1250.00 | 30 | | BLAKE | 2850.00 | 30 | | TURNER | 1500.00 | 30 | | JAMES | 950.00 | 30 | +--------+---------+--------+ 6 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno > ANY(select deptno from emp where ename="SMITH" OR ename="MILLER" ); +--------+---------+--------+ | ename | sal | deptno | +--------+---------+--------+ | SMITH | 800.00 | 20 | | ALLEN | 1600.00 | 30 | | WARD | 1250.00 | 30 | | JONES | 2975.00 | 20 | | MARTIN | 1250.00 | 30 | | BLAKE | 2850.00 | 30 | | SCOTT | 3000.00 | 20 | | TURNER | 1500.00 | 30 | | ADAMS | 1100.00 | 20 | | JAMES | 950.00 | 30 |
  • 4. | FORD | 3000.00 | 20 | +--------+---------+--------+ 11 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno > ANY(select deptno from emp where ename="TURNER" OR ename="MILLER" ); +--------+---------+--------+ | ename | sal | deptno | +--------+---------+--------+ | SMITH | 800.00 | 20 | | ALLEN | 1600.00 | 30 | | WARD | 1250.00 | 30 | | JONES | 2975.00 | 20 | | MARTIN | 1250.00 | 30 | | BLAKE | 2850.00 | 30 | | SCOTT | 3000.00 | 20 | | TURNER | 1500.00 | 30 | | ADAMS | 1100.00 | 20 | | JAMES | 950.00 | 30 | | FORD | 3000.00 | 20 | +--------+---------+--------+ 11 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno > ANY(select deptno from emp where ename="TURNER" OR ename="SCOTT" ); +--------+---------+--------+ | ename | sal | deptno | +--------+---------+--------+ | ALLEN | 1600.00 | 30 | | WARD | 1250.00 | 30 | | MARTIN | 1250.00 | 30 | | BLAKE | 2850.00 | 30 | | TURNER | 1500.00 | 30 | | JAMES | 950.00 | 30 | +--------+---------+--------+ 6 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where deptno > all(select deptno from emp where ename="TURNER" OR ename="SCOTT" ); Empty set (0.00 sec) mysql> select ename,sal,deptno from emp where sal > ANY(select sal from emp where ename="TURNER" OR ename="SCOTT" ); +-------+---------+--------+ | ename | sal | deptno | +-------+---------+--------+ | ALLEN | 1600.00 | 30 | | JONES | 2975.00 | 20 | | BLAKE | 2850.00 | 30 | | CLARK | 2450.00 | 10 | | SCOTT | 3000.00 | 20 | | KING | 5000.00 | 10 | | FORD | 3000.00 | 20 | +-------+---------+--------+ 7 rows in set (0.00 sec) mysql> select ename,sal,deptno from emp where sal > ALL(select sal from emp where ename="TURNER" OR ename="SCOTT" ); +-------+---------+--------+ | ename | sal | deptno | +-------+---------+--------+ | KING | 5000.00 | 10 | +-------+---------+--------+
  • 5. 1 row in set (0.00 sec) mysql> select ename,deptno from emp where deptno=(select deptno from dept where dname="RESEARCH"); +-------+--------+ | ename | deptno | +-------+--------+ | SMITH | 20 | | JONES | 20 | | SCOTT | 20 | | ADAMS | 20 | | FORD | 20 | +-------+--------+ 5 rows in set (0.00 sec) ##Jion mysql> create table orange(o_id varchar(20),price int(12)); Query OK, 0 rows affected (0.35 sec) mysql> create table apple(a_id varchar(20),price int(12)); Query OK, 0 rows affected (0.06 sec) mysql> insert into orange values("O1",50),("O2",60); Query OK, 2 rows affected (0.30 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> insert into apple values("A1",60),("A2",60); Query OK, 2 rows affected (0.30 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from apple; +------+-------+ | a_id | price | +------+-------+ | A1 | 60 | | A2 | 60 | +------+-------+ 2 rows in set (0.00 sec) mysql> select * from orange; +------+-------+ | o_id | price | +------+-------+ | O1 | 50 | | O2 | 60 | +------+-------+ 2 rows in set (0.00 sec) mysql> select * from orange,apple; +------+-------+------+-------+ | o_id | price | a_id | price | +------+-------+------+-------+ | O1 | 50 | A1 | 60 | | O2 | 60 | A1 | 60 | | O1 | 50 | A2 | 60 | | O2 | 60 | A2 | 60 | +------+-------+------+-------+ 4 rows in set (0.00 sec) mysql> select * from orange o inner join apple a on o.price=a.price; +------+-------+------+-------+ | o_id | price | a_id | price | +------+-------+------+-------+
  • 6. | O2 | 60 | A1 | 60 | | O2 | 60 | A2 | 60 | +------+-------+------+-------+ 2 rows in set (0.04 sec) mysql> select * from orange o left outer join apple a on o.price=a.price; +------+-------+------+-------+ | o_id | price | a_id | price | +------+-------+------+-------+ | O2 | 60 | A1 | 60 | | O2 | 60 | A2 | 60 | | O1 | 50 | NULL | NULL | +------+-------+------+-------+ 3 rows in set (0.00 sec) mysql> select * from orange o right outer join apple a on o.price=a.price; +------+-------+------+-------+ | o_id | price | a_id | price | +------+-------+------+-------+ | O2 | 60 | A1 | 60 | | O2 | 60 | A2 | 60 | +------+-------+------+-------+ 2 rows in set (0.00 sec) mysql> (select * from orange o left outer join apple a on o.price=a.price) union(select * from orange o right outer join apple a on o.price=a.price); +------+-------+------+-------+ | o_id | price | a_id | price | +------+-------+------+-------+ | O2 | 60 | A1 | 60 | | O2 | 60 | A2 | 60 | | O1 | 50 | NULL | NULL | +------+-------+------+-------+