Unit IV - 240223 - 105854
Unit IV - 240223 - 105854
Subqueries in Oracle
Unit IV
Sorting and Grouping Data in SQL & Joining Tables &
Subqueries in Oracle
* ORDER BY clause –
The ORDER BY clause is used to sort the records in your result set. The ORDER BY
clause can only be used in SELECT statements.
Syntax –
SELECT column1, column2, …… FROM tbl_name
[WHERE condition(s)]
ORDER BY column1, column2, …… [ASC | DESC];
RN NAME CITY
2 BALAJI NANDED
4 DIPAK UDGIR
1 AMOL LATUR
3 CHETAN PUNE
5 HEMANT LATUR
Example – 1) Show the details of the student according to the city’s name.
RN NAME CITY
1 AMOL LATUR
5 HEMANT LATUR
2 BALAJI NANDED
3 CHETAN PUNE
4 DIPAK UDGIR
2) Show the details of the student according to the city’s name in descending
order.
RN NAME CITY
4 DIPAK UDGIR
3 CHETAN PUNE
2 BALAJI NANDED
5 HEMANT LATUR
1 AMOL LATUR
Note: If the ASC or DESC modifier is not provided in the ORDER BY clause, the
results will be sorted by column in ascending order.
RN NAME CITY
1 AMOL LATUR
2 BALAJI NANDED
3 CHETAN PUNE
4 DIPAK UDGIR
5 HEMANT LATUR
* GROUP BY clause –
The GROUP BY clause is used in a SELECT statement to collect data across multiple
records and group the results by one or more columns.
Syntax –
SELECT column1, column2, ... , columnN
aggregate_function (aggregate_column)
FROM tbl_name
[WHERE condition(s)]
GROUP BY column1, column2, ... , columnN;
Example – Find out how many students are there in each city.
SQL > SELECT CITY, COUNT (RN) AS "NO. OF STUDENTS" FROM STUD GROUP BY
CITY;
* HAVING clause –
The HAVING clause is used in combination with the GROUP BY clause to restrict
the groups of returned rows to only those whose the condition is TRUE.
Syntax –
The syntax for the HAVING clause in Oracle/PLSQL is:
SQL > SELECT CITY, COUNT (RN) AS "NO. OF STUDENTS" FROM STUD
GROUP BY CITY HAVING COUNT (RN) > 1;
What is JOIN?
A join is a query that combines rows from two or more tables or views. Join clause
use when select records rows from two or more tables from the database. It’s depending
on certain columns from two tables. Matching columns are evaluate and if predicated
TRUE return a records set data in specified format.
SQL Join four different types: Inner Join, Outer Join, Self-Join, Cross Join.
Considering following type of Join visually.
* INNER JOIN –
Inner Join is the simplest and most common type of join
Syntax –
Example – List the supplier details along with order numbers to which they are belong
Output –
SUPPLIER_ID SUPPLIER_PRODUCT SUPPLIER_CITY ORDER_NO
1 SHOES LATUR 101
2 COMPUTERS PUNE 102
Above example will return all rows from "suppliers" and "orders" table where
there is a matching “supplier_id” value in both the “suppliers” and “orders” tables.
* EQUI JOIN –
Equi join is a specific type comparison base join (equally comparison) not allowing
other comparison operator such as <, > <= etc. And create record set results that are
combining columns value from the tables (two or more table).
Example – The following examples return the first name and job of each employee and
the number and name of the department in which the employee works.
Output –
Equi join use JOIN keyword specify table name and ON keyword specify the join
predicate condition.
Example –
SQL > SELECT * FROM SUPPLIERS S JOIN ORDERS O
ON S.SUPPLIER_ID = O.SUPPLIER_ID;
Output –
If join predicate condition both table column name are same, then you can write
this query shorthand way by using USING Keyword.
Example –
SQL > SELECT * FROM SUPPLIERS JOIN ORDERS
USING (SUPPLIER_ID);
Output –
* Natural join –
Natural join is a same as Equi join but different is resulting contains allow only one
column for each pair of same columns named. Record set contains haven't same name
columns are found.
Syntax –
Natural join query use NATURAL JOIN keyword to specify table name.
Example –
Output –
SUPPLIER_ID SUPPLIER_PRODUCT SUPPLIER_CITY ORDER_NO
1 SHOES LATUR 101
2 COMPUTERS PUNE 102
* OUTER JOIN –
An outer join is similar to equijoin but it gets also the non-matched rows from the
table. It is categorized in Left Outer Join, Right Outer Join and Full Outer Join by Oracle 9i
ANSI/ISO 1999 standard.
Syntax –
Example – List the supplier details along with the order details (if any) using Left Outer
Join.
Output –
Above example would return all records from the left table i.e. “suppliers” and only
those records from the right table i.e. “orders” where the join fields are equal.
Syntax –
Example – List the order details along with the supplier details (if any) using Right Outer
Join.
Output –
Above example would return all rows from the right table i.e. “orders” table and
only those rows from the left table i.e. “suppliers” table where the join condition is met.
Syntax –
SELECT column1, column2, …
FROM table1 FULL OUTER JOIN table2
ON table1.column = table2.column;
Example – This following query retrieves all rows in the SUPPLIERS table, even if there is
no match in the ORDERS table. It also retrieves all rows in the ORDERS table, even if there
is no match in the SUPPLIERS table.
Output –
Above example will return all rows from the left table i.e. “suppliers” table and all
rows from the right table i.e. “orders” table and whenever the join condition is not met,
it places the NULL value.
Syntax –
SQL > SELECT * FROM table1 CROSS JOIN table2;
Or
SQL > SELECT * FROM table1, table2;
Example –
Or
Output –
* SELF JOIN –
Self-Join is a specific type of Join. In Self Join, a table is joined with itself. A self-join
simply specifies that each rows of a table is combined with itself and every other row of
the table.
Syntax –
Example – Display all customer details that live in the city where AMOL lives.
Table – CUSTOMERS
Output –
Above example we are use A and B table alias name for CUSTOMERS table.
1) Region
Region_Id Region_Name
R101 Latur
R102 Pune
2) Country
Country_Id Country_Name Region_Id
3) Location
Location_Id City Country_Id
•To execute a join of three or more tables, Oracle first joins two of the tables based on
the join conditions comparing their columns and then joins the result to another table
based on join conditions containing columns of the joined tables and the new table.
•Oracle continues this process until all tables are joined into the result.
•For Example
SQL > SELECT R.REGION_NAME, C.COUNTRY_NAME, L.CITY
FROM REGIONS R, COUNTRIES C, LOCATIONS L
WHERE R.REGION_ID=C.REGION_ID
AND
C.COUNTRY_ID=L.COUNTRY_ID;
Output:-
2) if you do not know their names, then to get their id's you need to write the query in
this manner,
Subquery Output:
id first_name
-------- -------------
100 Rahul
102 Stephen
In the above sql statement, first the inner query is processed first and then the outer
query is processed.
SQL Subquery; INSERT Statement
Types of Subquery : -
A query is called correlated subquery when both the inner query and the outer query
are interdependent.