SQL Queries Using Set Operators, Joins and Subqueries Samples
SQL Queries Using Set Operators, Joins and Subqueries Samples
Bus_code(primary key) number 2. Bus_desc varchar Bus-route 1. Route_id(primary key) number 2. Route_no(unique) number 3. Bus_code(foreign key) number 4. Origin Varchar 5. destination varchar 6. fare number 7. dist number 8. capacity number Journey 1. Journey_id(primary key) number 2. date of Journey(Not Null) date 3. Time(Not Null) varchar2 4. Route_id(foreign key) number 5. Bus_code(foreign key) number 1. Journey_id(foreign key) number 2. Ticket_no(primary key) number 3. Date of Birth date Ticket 4. Date of Journey date 5. Time(Not Null) varchar2 6. Station varchar 7. Origin(Not Null) Varchar 8. destination(Not Null) varchar 9. Adults number 10. Total fare number 11. Route_id(foreign key) number
Ticket_detail 1. Ticket_no number 2. Name varchar 3. Sex char 4. Age number 5. Fare number TO CREATE TABLES Bus-details SQL> create table Bus_details 2 (Bus_code number(4), 3 Bus_desc varchar(10), 4 CONSTRAINT buscode_pk PRIMARY KEY(Bus_code)); Table created. SQL> desc Bus_details Name Null? Type ----------------------------------------- -------- ---------------------------BUS_CODE NOT NULL NUMBER(4) BUS_DESC VARCHAR2(10) Bus-route SQL> create table bus_route 2 (Route_id number(4), 3 Route_no number(5), 4 Bus_code number(4),Origin Varchar2(10), 5 destination varchar2(10), 6 fare number(4), 7 dist number(4), 8 capacity number(3), 9 CONSTRAINT Route_id_pk PRIMARY KEY(Route_id), 10 CONSTRAINT Route_no_uk UNIQUE(Route_no), 11 CONSTRAINT Bus_code_fk FOREIGN KEY(Bus_code) 12 REFERENCES Bus_details(Bus_code)); Table created.
SQL> DESC bus_route; Name Null? Type ----------------------------------------- -------- ---------------------------ROUTE_ID NOT NULL NUMBER(4) ROUTE_NO NUMBER(5) BUS_CODE NUMBER(4) ORIGIN VARCHAR2(10) DESTINATION VARCHAR2(10) FARE NUMBER(4) DIST NUMBER(4) CAPACITY NUMBER(3)
Journey SQL> create table Journey 2 (Journey_id number(4), 3 dojo date NOT NULL, 4 Time varchar2(10) NOT NULL, 5 Route_id number(4), 6 Bus_code NUMBER(4), 7 CONSTRAINT Journey_id_pk PRIMARY KEY(Journey_id), 8 CONSTRAINT Route_id_fk FOREIGN KEY(Route_id) 9 REFERENCES Bus_route(Route_id), 10 CONSTRAINT Bus_cd_fk FOREIGN KEY(Bus_code) 11 REFERENCES Bus_details(Bus_code)); Table created. SQL> DESC Journey; Name Null? Type ----------------------------------------- -------- ---------------------------JOURNEY_ID NOT NULL NUMBER(4) DOJO NOT NULL DATE TIME NOT NULL VARCHAR2(10) ROUTE_ID NUMBER(4) BUS_CODE NUMBER(4)
Ticket SQL> create table Ticket 2 (Journey_id number(4), 3 Ticket_no number(4), 4 dob date 5 ,doj date, 6 time varchar2(10) NOT NULL, 7 Station varchar(10), 8 Origin varchar(10) NOT NULL, 9 dest varchar2(8) NOT NULL, 10 Adults number(2), 11 Total_fare number(4), 12 Route_id NUMBER(4), 13 CONSTRAINT Ticket_no_pk PRIMARY KEY(Ticket_no), 14 CONSTRAINT Journey_id_fk FOREIGN KEY(Journey_id) 15 REFERENCES Journey(Journey_id), 16 CONSTRAINT Rout_id_fk FOREIGN KEY(Route_id) 17 REFERENCES Bus_route(Route_id)); Table created. Ticket_detail SQL> create table Ticketdetail 2 (Ticket_no number(3), 3 Name varchar(10), 4 Sex char(1), 5 Age number(3), 6 Fare number(4)); Table created
SQL> insert into Bus_details values(&Bus_code,'&Bus_desc'); Enter value for bus_code: 1000 Enter value for bus_desc: acexpres old 1: insert into Bus_details values(&Bus_code,'&Bus_desc') new 1: insert into Bus_details values(1000,'acexpres') 1 row created. SQL> / Enter value for bus_code: 1001 Enter value for bus_desc: ultradels old 1: insert into Bus_details values(&Bus_code,'&Bus_desc') new 1: insert into Bus_details values(1001,'ultradels') 1 row created. SQL> / Enter value for bus_code: 2000 Enter value for bus_desc: express old 1: insert into Bus_details values(&Bus_code,'&Bus_desc') new 1: insert into Bus_details values(2000,'express') 1 row created. SQL> / Enter value for bus_code: 2001 Enter value for bus_desc: suprfast old 1: insert into Bus_details values(&Bus_code,'&Bus_desc') new 1: insert into Bus_details values(2001,'suprfast') 1 row created.
SQL> / Enter value for bus_code: 3000 Enter value for bus_desc: suprdelx old 1: insert into Bus_details values(&Bus_code,'&Bus_desc') new 1: insert into Bus_details values(3000,'suprdelx') 1 row created. SQL> / Enter value for bus_code: 3001 Enter value for bus_desc: deluxe old 1: insert into Bus_details values(&Bus_code,'&Bus_desc') new 1: insert into Bus_details values(3001,'deluxe') 1 row created. SQL> select * from Bus_details; BUS_CODE BUS_DESC ---------- ---------1000 acexpres 1001 ultradels 2000 express 2001 suprfast 3000 suprdelx 3001 deluxe 6 rows selected.
SQL> insert into bus_route values(&Route_id,&Route_no,&Bus_code,'&Origin','&destination',&fare,&di st,&capacity); Enter value for route_id: 100 Enter value for route_no: 1 Enter value for bus_code: 1000 Enter value for origin: bangalore Enter value for destination: chennai Enter value for fare: 1400 Enter value for dist: 320 Enter value for capacity: 32 old 1: insert into bus_route values(&Route_id,&Route_no,&Bus_code,'&Origin','&destination',&fare,&di st,&capacity) new 1: insert into bus_route values(100,1,1000,'bangalore','chennai',1400,320,32) 1 row created. SQL> / Enter value for route_id: 101 Enter value for route_no: 2 Enter value for bus_code: 3000 Enter value for origin: chennai Enter value for destination: kerala Enter value for fare: 1200 Enter value for dist: 420 Enter value for capacity: 40 old 1: insert into bus_route values(&Route_id,&Route_no,&Bus_code,'&Origin','&destination',&fare,&di st,&capacity) new 1: insert into bus_route values(101,2,3000,'chennai','kerala',1200,420,40) 1 row created.
SQL> / Enter value for route_id: 102 Enter value for route_no: 3 Enter value for bus_code: 2000 Enter value for origin: chennai Enter value for destination: bangalore Enter value for fare: 1230 Enter value for dist: 320 Enter value for capacity: 30 old 1: insert into bus_route values(&Route_id,&Route_no,&Bus_code,'&Origin','&destination',&fare,&di st,&capacity) new 1: insert into bus_route values(102,3,2000,'chennai','bangalore',1230,320,30) 1 row created. SQL> / Enter value for route_id: 103 Enter value for route_no: 4 Enter value for bus_code: 1001 Enter value for origin: coimbatore Enter value for destination: trichy Enter value for fare: 705 Enter value for dist: 465 Enter value for capacity: 45 old 1: insert into bus_route values(&Route_id,&Route_no,&Bus_code,'&Origin','&destination',&fare,&di st,&capacity) new 1: insert into bus_route values(103,4,1001,'coimbatore','trichy',705,465,45) 1 row created.
SQL> / Enter value for route_id: 104 Enter value for route_no: 5 Enter value for bus_code: 2001 Enter value for origin: trichy Enter value for destination: madurai Enter value for fare: 680 Enter value for dist: 205 Enter value for capacity: 35 old 1: insert into bus_route values(&Route_id,&Route_no,&Bus_code,'&Origin','&destination',&fare,&di st,&capacity) new 1: insert into bus_route values(104,5,2001,'trichy','madurai',680,205,35) 1 row created. SQL> / Enter value for route_id: 105 Enter value for route_no: 6 Enter value for bus_code: 3001 Enter value for origin: theni Enter value for destination: erode Enter value for fare: 500 Enter value for dist: 150 Enter value for capacity: 40 old 1: insert into bus_route values(&Route_id,&Route_no,&Bus_code,'&Origin','&destination',&fare,&di st,&capacity) new 1: insert into bus_route values(105,6,3001,'theni','erode',500,150,40) 1 row created.
SQL> select * from bus_route; ROUTE_ID ROUTE_NO BUS_CODE ORIGIN DESTINATIO FARE DIST ---------- ---------- ---------- ---------- ---------- ---------- ---------CAPACITY ---------100 1 1000 bangalore chennai 1400 320 32 101 40 102 30 2 3000 chennai kerala 1200 420
1230
320
ROUTE_ID ROUTE_NO BUS_CODE ORIGIN DESTINATIO DIST ---------- ---------- ---------- ---------- ---------- ---------- ---------CAPACITY ---------103 4 1001 coimbatore trichy 705 465 45 104 35 105 40 5 2001 trichy madurai 680 205
FARE
3001 theni
erode
500
150
6 rows selected.
SQL> insert into Journey values(&Journey_id,'&dojo','&Time',&Route_id,&Bus_code); Enter value for journey_id: 300 Enter value for dojo: 11-jan-10 Enter value for time: 10.30 Enter value for route_id: 100 Enter value for bus_code: 1000 old 1: insert into Journey values(&Journey_id,'&dojo','&Time',&Route_id,&Bus_code) new 1: insert into Journey values(300,'11-jan-10','10.30',100,1000) 1 row created. SQL> / Enter value for journey_id: 301 Enter value for dojo: 15-may-06 Enter value for time: 21.35 Enter value for route_id: 101 Enter value for bus_code: 3000 old 1: insert into Journey values(&Journey_id,'&dojo','&Time',&Route_id,&Bus_code) new 1: insert into Journey values(301,'15-may-06','21.35',101,3000) 1 row created. SQL> / Enter value for journey_id: 302 Enter value for dojo: 23-apr-10 Enter value for time: 23.40 Enter value for route_id: 102 Enter value for bus_code: 2000 old 1: insert into Journey values(&Journey_id,'&dojo','&Time',&Route_id,&Bus_code) new 1: insert into Journey values(302,'23-apr-10','23.40',102,2000) 1 row created.
SQL> / Enter value for journey_id: 303 Enter value for dojo: 16-nov-00 Enter value for time: 11.30 Enter value for route_id: 103 Enter value for bus_code: 1001 old 1: insert into Journey values(&Journey_id,'&dojo','&Time',&Route_id,&Bus_code) new 1: insert into Journey values(303,'16-nov-00','11.30',103,1001) 1 row created. SQL> / Enter value for journey_id: 304 Enter value for dojo: 12-feb-03 Enter value for time: 13.25 Enter value for route_id: 104 Enter value for bus_code: 2001 old 1: insert into Journey values(&Journey_id,'&dojo','&Time',&Route_id,&Bus_code) new 1: insert into Journey values(304,'12-feb-03','13.25',104,2001) 1 row created. SQL> / Enter value for journey_id: 305 Enter value for dojo: 18-dec-11 Enter value for time: 12.50 Enter value for route_id: 105 Enter value for bus_code: 3001 old 1: insert into Journey values(&Journey_id,'&dojo','&Time',&Route_id,&Bus_code) new 1: insert into Journey values(305,'18-dec-11','12.50',105,3001) 1 row created.
SQL> select * from Journey; JOURNEY_ID DOJO TIME ROUTE_ID BUS_CODE ---------- --------- ---------- ---------- ---------300 11-JAN-10 10.30 100 1000 301 15-MAY-06 21.35 101 3000 302 23-APR-10 23.40 102 2000 303 16-NOV-00 11.30 103 1001 304 12-FEB-03 13.25 104 2001 305 18-DEC-11 12.50 105 3001 6 rows selected. SQL> insert into Ticket values(&Journey_id,&Ticket_no,'&dob','&doj','&time','&Station','&Origin','& dest',&Adults,&Total_fare,&Route_id); Enter value for journey_id: 300 Enter value for ticket_no: 50 Enter value for dob: 12-jun-83 Enter value for doj: 15-jan-09 Enter value for time: 23.30 Enter value for station: ulsoor Enter value for origin: bangalore Enter value for dest: chennai Enter value for adults: 20 Enter value for total_fare: 800 Enter value for route_id: 100 old 1: insert into Ticket values(&Journey_id,&Ticket_no,'&dob','&doj','&time','&Station','&Origin','& dest',&Adults,&Total_fare,&Route_id) new 1: insert into Ticket values(300,50,'12-jun-83','15-jan09','23.30','ulsoor','bangalore','chennai',20,800,100) 1 row created.
SQL> / Enter value for journey_id: 301 Enter value for ticket_no: 51 Enter value for dob: 14-jan-56 Enter value for doj: 21-mar-12 Enter value for time: 12,55 Enter value for station: munar Enter value for origin: kerala Enter value for dest: chennai Enter value for adults: 21 Enter value for total_fare: 1200 Enter value for route_id: 101 old 1: insert into Ticket values(&Journey_id,&Ticket_no,'&dob','&doj','&time','&Station','&Origin','& dest',&Adults,&Total_fare,&Route_id) new 1: insert into Ticket values(301,51,'14-jan-56','21-mar12','12,55','munar','kerala','chennai',21,1200,101) 1 row created. SQL> / Enter value for journey_id: 302 Enter value for ticket_no: 52 Enter value for dob: 13-apr-69 Enter value for doj: 12-mar-06 Enter value for time: 1.00 Enter value for station: koymbdu Enter value for origin: chennai Enter value for dest: banglore Enter value for adults: 19 Enter value for total_fare: 1320 Enter value for route_id: 102
old 1: insert into Ticket values(&Journey_id,&Ticket_no,'&dob','&doj','&time','&Station','&Origin','& dest',&Adults,&Total_fare,&Route_id) new 1: insert into Ticket values(302,52,'13-apr-69','12-mar06','1.00','koymbdu','chennai','banglore',19,1320,102) 1 row created. SQL> / Enter value for journey_id: 303 Enter value for ticket_no: 53 Enter value for dob: 19-feb-90 Enter value for doj: 04-sep-04 Enter value for time: 14.15 Enter value for station: cmbstnd Enter value for origin: coimbatore Enter value for dest: trichy Enter value for adults: 12 Enter value for total_fare: 800 Enter value for route_id: 103 old 1: insert into Ticket values(&Journey_id,&Ticket_no,'&dob','&doj','&time','&Station','&Origin','& dest',&Adults,&Total_fare,&Route_id) new 1: insert into Ticket values(303,53,'19-feb-90','04-sep04','14.15','cmbstnd','coimbatore','trichy',12,800,103) 1 row created. SQL> / Enter value for journey_id: 304 Enter value for ticket_no: 54 Enter value for dob: 17-jan-70 Enter value for doj: 03-jun-99 Enter value for time: 12.50 Enter value for station: srirangam
Enter value for origin: trichy Enter value for dest: madurai Enter value for adults: 23 Enter value for total_fare: 1400 Enter value for route_id: 104 old 1: insert into Ticket values(&Journey_id,&Ticket_no,'&dob','&doj','&time','&Station','&Origin','& dest',&Adults,&Total_fare,&Route_id) new 1: insert into Ticket values(304,54,'17-jan-70','03-jun99','12.50','srirangam','trichy','madurai',23,1400,104) 1 row created. SQL> / Enter value for journey_id: 305 Enter value for ticket_no: 55 Enter value for dob: 18-oct-53 Enter value for doj: 23-feb-12 Enter value for time: 13.05 Enter value for station: thbstnd Enter value for origin: theni Enter value for dest: erode Enter value for adults: 15 Enter value for total_fare: 1250 Enter value for route_id: 105 old 1: insert into Ticket values(&Journey_id,&Ticket_no,'&dob','&doj','&time','&Station','&Origin','& dest',&Adults,&Total_fare,&Route_id) new 1: insert into Ticket values(305,55,'18-oct-53','23-feb12','13.05','thbstnd','theni','erode',15,1250,105) 1 row created.
SQL> select * from Ticket; JOURNEY_ID TICKET_NO DOB DOJ TIME STATION ORIGIN ---------- ---------- --------- --------- ---------- ---------- ---------DEST ADULTS TOTAL_FARE ROUTE_ID -------- ---------- ---------- ---------300 50 12-JUN-83 15-JAN-09 23.30 ulsoor bangalore chennai 20 800 100 301 chennai 302 banglore 51 14-JAN-56 21-MAR-12 12,55 21 1200 101 52 13-APR-69 12-MAR-06 1.00 19 1320 102 munar kerala
koymbdu chennai
JOURNEY_ID TICKET_NO DOB DOJ TIME STATION ORIGIN ---------- ---------- --------- --------- ---------- ---------- ---------DEST ADULTS TOTAL_FARE ROUTE_ID -------- ---------- ---------- ---------303 53 19-FEB-90 04-SEP-04 14.15 cmbstnd coimbatore trichy 12 800 103 304 madurai 305 erode 54 17-JAN-70 03-JUN-99 12.50 23 1400 104 55 18-OCT-53 23-FEB-12 13.05 15 1250 105 srirangam trichy
thbstnd theni
6 rows selected.
SQL> insert into ticketdetail values(&Ticket_no,'&Name','&Sex',&Age,&Fare); Enter value for ticket_no: 50 Enter value for name: priya Enter value for sex: f Enter value for age: 19 Enter value for fare: 1400 old 1: insert into ticketdetail values(&Ticket_no,'&Name','&Sex',&Age,&Fare) new 1: insert into ticketdetail values(50,'priya','f',19,1400) 1 row created. SQL> / Enter value for ticket_no: 51 Enter value for name: john Enter value for sex: m Enter value for age: 24 Enter value for fare: 1110 old 1: insert into ticketdetail values(&Ticket_no,'&Name','&Sex',&Age,&Fare) new 1: insert into ticketdetail values(51,'john','m',24,1110) 1 row created. SQL> / Enter value for ticket_no: 52 Enter value for name: sheela Enter value for sex: f Enter value for age: 35 Enter value for fare: 800 old 1: insert into ticketdetail values(&Ticket_no,'&Name','&Sex',&Age,&Fare) new 1: insert into ticketdetail values(52,'sheela','f',35,800) 1 row created.
SQL> / Enter value for ticket_no: 53 Enter value for name: ranjani Enter value for sex: f Enter value for age: 18 Enter value for fare: 900 old 1: insert into ticketdetail values(&Ticket_no,'&Name','&Sex',&Age,&Fare) new 1: insert into ticketdetail values(53,'ranjani','f',18,900) 1 row created. SQL> / Enter value for ticket_no: 54 Enter value for name: vijaya Enter value for sex: f Enter value for age: 42 Enter value for fare: 1200 old 1: insert into ticketdetail values(&Ticket_no,'&Name','&Sex',&Age,&Fare) new 1: insert into ticketdetail values(54,'vijaya','f',42,1200) 1 row created. SQL> / Enter value for ticket_no: 55 Enter value for name: arun Enter value for sex: m Enter value for age: 25 Enter value for fare: 850 old 1: insert into ticketdetail values(&Ticket_no,'&Name','&Sex',&Age,&Fare) new 1: insert into ticketdetail values(55,'arun','m',25,850) 1 row created.
SQL> select * from ticketdetail; TICKET_NO NAME S AGE FARE ---------- ---------- - ---------- ---------50 priya f 19 1400 51 john m 24 1110 52 sheela f 35 800 53 ranjani f 18 900 54 vijaya f 42 1200 55 arun m 25 850 6 rows selected.
QUESTIONS 1. Display the bus description which is having the least capacity. SQL> select bus_desc from bus_details d1,bus_route d2 where(d1.bus_code=d2.bus_code) and (d2.capacity=(select min(capacity) from bus_route)); BUS_DESC ---------express 2. How many buses are having destination as Chennai? SQL> select count(*) from bus_route where destination='chennai'; COUNT(*) ---------1 3.How many passengers are traveling below 21 years of age? SQL> select count(*) from ticketdetail where age<21; COUNT(*) ---------2 4.Display the bus description which is having the highest fare. SQL> select bus_desc from bus_details d1,bus_route d2 where (d1.bus_code=d2.bus_code) and d2.fare=(select max(fare) from bus_route);
BUS_DESC ---------acexpres 5. Display the names of the passengers who have booked their ticket in the month of January. SQL> select name from ticketdetail d1,ticket d2 where (d1.ticket_no=d2.ticket_no) and doj like '%JAN%'; NAME ---------priya john 6. Display the description and Bus Code of the bus whose fare is greater than the average fare in the table. SQL> select d1.bus_code,d1.bus_desc from bus_details d1,bus_route d2 where(d1.bus_code=d2.bus_code) and d2.fare>(select avg(fare) from bus_route); BUS_CODE BUS_DESC ---------- ---------1000 acexpres 3000 suprdelx 2000 express 7. How many female passengers are traveling in the Deluxe Bus? SQL> select count(sex) from ticketdetail d1,ticket d2,bus_route d3,bus_details d4 where (d1.ticket_no=d2.ticket_no) and (d2.route_id=d3.route_id) and (d3.bus_code=d4.bus_ code) and d1.sex='f' and d4.bus_desc='ultradels'; (or) SQL> select count(sex) from ticketdetail where sex='f' and ticket_no in(select ticket_no from ticket where route_id in(select route_id from bus_route where bus_code in(select bus_code from bus_details where bus_desc='ultradels')));
COUNT(SEX) ---------1 8. How many male passengers are traveling in the Super Fast Bus? SQL> select count(sex) from ticketdetail where sex='m' and ticket_no in(select ticket_no from ticket where route_id in(select route_id from bus_route where bus_code in(sele ct bus_code from bus_details where bus_desc='suprdelx'))); (or) SQL> select count(sex) from ticketdetail d1,ticket d2,bus_route d3,bus_details d4 where (d1.ticket_no=d2.ticket_no) and (d2.route_id=d3.route_id) and (d3.bus_code=d4.bus_c de) and d1.sex='m' and d4.bus_desc='suprdelx'; COUNT(SEX) ---------1 9. Display the names of the passengers who departure from Bangalore. SQL> select d1.name from ticketdetail d1,ticket d2 where (d1.ticket_no=d2.ticket_no) and d2.origin='bangalore'; NAME ---------priya 10. Display the journey time of the passenger "John". SQL> select d1.time from ticket d1,ticketdetail d2 where (d1.ticket_no=d2.ticket_no) and d2.name='john'; TIME ---------12.55
11. Display the bus description which are neither originating from Chennai nor reaching Chennai. SQL> select d1.bus_desc from bus_details d1,bus_route d2 where (d1.bus_code=d2.bus_code) and d2.origin<>'chennai' and d2.destination<>'chennai'; BUS_DESC ---------ultradels suprfast deluxe 12. Select rows from Bus route such that the route id's are greater than any of the ticket nos with J_id as 02 in the journey table. SQL> select * from bus_route where route_id in(select route_id from journey where route_id in(select route_id from ticket where route_id>any(ticket_no) and journey_id=302));
ROUTE_ID ROUTE_NO BUS_CODE ORIGIN DESTINATIO FARE DIST
---------CAPACITY
---------3
---------2000 chennai
----------
---------102 30
bangalore
13. Select rows from Bus route such that the route id's are greater than all the ticket nos with J_id as 02 in the journey table. SQL> select * from bus_route where route_id in(select route_id from journey where route_id in(select route_id from ticket where route_id>all(ticket_no) and journey_id=302));
ROUTE_ID ROUTE_NO BUS_CODE ORIGIN DESTINATIO FARE DIST
----------
----------
----------
----------
CAPACITY
---------102 3 2000 chennai bangalore 1230 320 30 14. Select rows from ticket such that the ticket number exceeds the average of the total fare and the origin for such number should be Chennai. SQL> select * from ticket where ticket_no<(select avg(total_fare) from ticket) and origin='chennai';
JOURNEY_ID TICKET_NO DOB DOJ TIME STATION ORIGIN
---------DEST --------
----------
---------
---------
----------
----------
----------
302 banglore
koymbdu
chennai
15. Select distinct route id's from bus route and ticket tables. SQL> select distinct route_id from bus_route intersect select distinct route_id from ticket; ROUTE_ID ---------100 101 102 103 104 105 6 rows selected.