0% found this document useful (0 votes)
13 views4 pages

Exp 7 Subqueries

The document outlines an experiment focused on executing SQL commands for subqueries, detailing the creation of two tables, EMP2 and DEPT2, and inserting records into them. It provides step-by-step procedures for creating tables, inserting data, and executing nested queries to extract information. The final output demonstrates the successful execution of a nested query that retrieves employee names based on specific salary conditions.

Uploaded by

choleacademy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Exp 7 Subqueries

The document outlines an experiment focused on executing SQL commands for subqueries, detailing the creation of two tables, EMP2 and DEPT2, and inserting records into them. It provides step-by-step procedures for creating tables, inserting data, and executing nested queries to extract information. The final output demonstrates the successful execution of a nested query that retrieves employee names based on specific salary conditions.

Uploaded by

choleacademy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment NO: 7 Write SQL statements to implement

Subqueries
(Nested Queries)

AIM
To execute and verify the SQL commands for Subqueries.

OBJECTIVE:
Subqueries can have more than one level of nesting in one single query. A
SQL nested
query is a SELECT query that is nested inside a SELECT, UPDATE, INSERT, or
DELETE SQL query.

PROCEDURE
STEP 1: Start
STEP 2: Create two different tables with its essential attributes.
STEP 3: Insert attribute values into the table.
STEP 4: Create the Nested query from the above created table.
STEP 5: Execute Command and extract information from the tables.
STEP 6: Stop

SQL COMMANDS
1. COMMAND NAME: SELECT
COMMAND DESCRIPTION: SELECT command is used to select records from
the table.
2. COMMAND NAME: WHERE
COMMAND DESCRIPTION: WHERE command is used to identify particular
elements.
3. COMMAND NAME: HAVING
COMMAND DESCRIPTION: HAVING command is used to identify particular
elements.
4. COMMAND NAME: MIN (SAL)
COMMAND DESCRIPTION: MIN (SAL) command is used to find minimum
salary

Table -1
SYNTAX FOR CREATING A TABLE:
SQL: CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1 <DATATYPE>
(SIZE), COLUMN NAME.1 <DATATYPE> (SIZE) ……………………………);

SQL> CREATE TABLE EMP2(EMPNO NUMBER(5),


ENAME VARCHAR2(20),
JOB VARCHAR2(20),
SAL NUMBER(6),
MGRNO NUMBER(4),
DEPTNO NUMBER(3));
SYNTAX FOR INSERT RECORDS IN TO A TABLE:
SQL :> INSERT INTO <TABLE NAME> VALUES< VAL1, ‘VAL2’,…..);

INSERTION
SQL> INSERT INTO EMP2 VALUES(1001,'MAHESH','PROGRAMMER',15000,1560,200);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1002,'MANOJ','TESTER',12000,1560,200);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1003,'KARTHIK','PROGRAMMER',13000,1400,201);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1004,'NARESH','CLERK',1400,1400,201);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1005,'MANI','TESTER',13000,1400,200);
1 ROW CREATED.

SQL> INSERT INTO EMP2 VALUES(1006,'VIKI','DESIGNER',12500,1560,201);


1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1007,'MOHAN','DESIGNER',14000,1560,201);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1008,'NAVEEN','CREATION',20000,1400,201);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1009,'PRASAD','DIR',20000,1560,202);
1 ROW CREATED.
SQL> INSERT INTO EMP2 VALUES(1010,'AGNESH','DIR',15000,1400,200);
1 ROW CREATED.

SYNTAX FOR SELECT RECORDS FROM THE TABLE:


SQL> SELECT * FROM <TABLE NAME>;

SQL> SELECT * FROM EMP2;

EMPNO ENAME JOB SAL MGRNO DPTNO


---------- ---------- ---------- ---------- ---------- ----------
1001 MAHESH PROGRAMMER 15000 1560 200
1002 MANOJ TESTER 12000 1560 200
1003 KARTHIK PROGRAMMER 13000 1400 201
1004 NARESH CLERK 1400 1400 201
1005 MANI TESTER 13000 1400 200
1006 VIKI DESIGNER 12500 1560 201
1007 MOHAN DESIGNER 14000 1560 201
1008 NAVEEN CREATION 20000 1400 201
1009 PRASAD DIR 20000 1560 202
1010 AGNESH DIR 15000 1400 200

TABLE- 2
SYNTAX FOR CREATING A TABLE:

SQL: CREATE <OBJ.TYPE> <OBJ.NAME> (COLUMN NAME.1 <DATATYPE>


(SIZE), COLUMN NAME.1 <DATATYPE> (SIZE) ……………………………);
SQL> CREATE TABLE DEPT2(DEPTNO NUMBER(3),
DEPTNAME VARCHAR2(10),
LOCATION VARCHAR2(15));
Table created.

SYNTAX FOR INSERT RECORDS IN TO A TABLE:


SQL :> INSERT INTO <TABLE NAME> VALUES< VAL1, ‘VAL2’,…..);

INSERTION
SQL> INSERT INTO DEPT2 VALUES(107,'DEVELOP','ADYAR');
1 ROW CREATED.
SQL> INSERT INTO DEPT2 VALUES(201,'DEBUG','UK');
1 ROW CREATED.
SQL> INSERT INTO DEPT2 VALUES(200,'TEST','US');
SQL> INSERT INTO DEPT2 VALUES(201,'TEST','USSR');
1 ROW CREATED.
SQL> INSERT INTO DEPT2 VALUES(108,'DEBUG','ADYAR');
1 ROW CREATED.
SQL> INSERT INTO DEPT2 VALUES(109,'BUILD','POTHERI');
1 ROW CREATED.

SYNTAX FOR SELECT RECORDS FROM THE TABLE:


SQL> SELECT * FROM <TABLE NAME>;

SQL> SELECT * FROM DEPT2;

DEPTNO DEPTNAME LOCATION


---------- ---------- ---------------
107 DEVELOP ADYAR
201 DEBUG UK
200 TEST US
201 TEST USSR
108 DEBUG ADYAR
109 BUILD POTHERI
6 rows selected.

GENERAL SYNTAX FOR NESTED QUERY:


SELECT "COLUMN_NAME1"
FROM "TABLE_NAME1"
WHERE "COLUMN_NAME2" [COMPARISON OPERATOR]
(SELECT "COLUMN_NAME3"
FROM "TABLE_NAME2"
WHERE [CONDITION])
SYNTAX NESTED QUERY STATEMENT:
SQL> SELECT <COLUMN_NAME> FROM FRORM <TABLE _1> WHERE
<COLUMN_NAME> <RELATIONAL _OPERATION> ‘VALUE’
(SELECT (AGGRECATE FUNCTION) FROM <TABLE_1> WHERE <COLUMN
NAME> = ‘VALUE’
(SELECT <COLUMN_NAME> FROM <TABLE_2> WHERE <COLUMN_NAME=
‘VALUE’));

NESTED QUERY STATEMENT:


SQL> SELECT ENAME FROM EMP2 WHERE SAL>
(SELECT MIN(SAL) FROM EMP2 WHERE DPTNO=
(SELECT DEPTNO FROM DEPT2 WHERE LOCATION='UK'));

Nested Query Output:

ENAME
----------
MAHESH
MANOJ
KARTHIK
MANI
VIKI
MOHAN
NAVEEN
PRASAD
AGNESH

Result: SQL Statements in Subqueries (nested queries) has been


executed

You might also like