The following code introduces the concept of subqueries by starting with an example of the IN operator.
You could execute the following query to get the desired result:
select attendee from registrations where course in ('JAVA','PLS','XML')
You can use subquery to replace the hard-coded list of courses.
select attendee from registrations where course in (select code from courses where category = 'BLD');
Oracle first substitutes the subquery between the parentheses with its result-a number of course codes-and then executes the main query.
To retrieve the correct number of emp, you should use SELECT DISTINCT in the main query to eliminate duplicates.
SQL> SQL>-- ww w . jav a 2 s .co m SQL> drop table registrations; Table dropped. SQL> create table registrations( 2 attendee NUMBER(4) not null, 3 course VARCHAR2(6) , 4 begindate DATE not null, 5 evaluation NUMBER(1) check (evaluation in (1,2,3,4,5))) ; SQL> SQL> insert into registrations values (7002,'SQL',date '1999-04-12',4 ); SQL> insert into registrations values (7934,'SQL',date '1999-04-12',5 ); SQL> insert into registrations values (7006,'SQL',date '1999-04-12',4 ); SQL> insert into registrations values (7011,'SQL',date '1999-04-12',2 ); SQL> insert into registrations values (7008,'SQL',date '1999-10-04',NULL); SQL> insert into registrations values (7009,'SQL',date '1999-10-04',3 ); SQL> insert into registrations values (7902,'SQL',date '1999-10-04',4 ); SQL> insert into registrations values (7902,'SQL',date '1999-12-13',NULL); SQL> insert into registrations values (7006,'SQL',date '1999-12-13',NULL); SQL> insert into registrations values (7003,'JSON',date '1999-08-10',4 ); SQL> insert into registrations values (7012,'JSON',date '1999-08-10',4 ); SQL> insert into registrations values (7902,'JSON',date '1999-08-10',5 ); SQL> insert into registrations values (7010,'JSON',date '2000-09-27',5 ); SQL> insert into registrations values (7002,'JAVA',date '1999-12-13',2 ); SQL> insert into registrations values (7007,'JAVA',date '1999-12-13',NULL ); SQL> insert into registrations values (7011,'JAVA',date '1999-12-13',5 ); SQL> insert into registrations values (7008,'JAVA',date '1999-12-13',5 ); SQL> insert into registrations values (7009,'JAVA',date '1999-12-13',4 ); SQL> insert into registrations values (7004,'JAVA',date '2000-02-01',3 ); SQL> insert into registrations values (7008,'JAVA',date '2000-02-01',4 ); SQL> insert into registrations values (7006,'JAVA',date '2000-02-01',5 ); SQL> insert into registrations values (7012,'XML',date '2000-02-03',4 ); SQL> insert into registrations values (7002,'XML',date '2000-02-03',5 ); SQL> insert into registrations values (7004,'PLS',date '2000-09-11',NULL); SQL> insert into registrations values (7002,'PLS',date '2000-09-11',NULL); SQL> insert into registrations values (7011,'PLS',date '2000-09-11',NULL); SQL> SQL> SQL> drop table courses; Table dropped. SQL> create table courses( 2 code VARCHAR2(6) primary key, 3 description VARCHAR2(30) not null, 4 category CHAR(3) not null, 5 duration NUMBER(2) not null) ; SQL> insert into courses values('SQL','Introduction to SQL', 'GEN',4); SQL> insert into courses values('JSON','Oracle for application users','GEN',1); SQL> insert into courses values('JAVA','Java for Oracle developers', 'BLD',4); SQL> insert into courses values('PLS','Introduction to PL/SQL', 'BLD',1); SQL> insert into courses values('XML','XML for Oracle developers', 'BLD',2); SQL> insert into courses values('ERM','Data modeling with ERM', 'DSG',3); SQL> insert into courses values('PMT','Process modeling techniques', 'DSG',1); SQL> insert into courses values('RSD','Relational system design', 'DEF',2); SQL> insert into courses values('PRO','Prototyping', 'DSG',5); SQL> insert into courses values('GEN','System generation', 'DSG',4); SQL> SQL> SQL> select attendee 2 from registrations 3 where course in (select code 4 from courses 5 where category = 'BLD'); ATTENDEE --------- 07002.00 07007.00 07011.00 07008.00 07009.00 07004.00 07008.00 07006.00 07012.00 07002.00 07004.00 ATTENDEE --------- 07002.00 07011.00 13 rows selected. SQL>