Rdbms unit 4 notes
Rdbms unit 4 notes
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 AMIR 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 AMIR 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 AMIR 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 AMIR 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)]
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:
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 –
SELECT * FROM table1 CROSS JOIN table2;
Or
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) Regions
Region_Id Region_Name
R101 East
R102 West
2) Countries
Country_Id Country_Name Region_Id
3) Locations
Location_Id City Country_Id
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:-
SQL Subquery: -
Subquery or Inner query or Nested query is a query in a query. SQL subquery is usually
added in the WHERE Clause of the SQL statement. Most of the time, a subquery is used
when you know how to search for a value using a SELECT statement, but do not know
the exact value in the database.
Subqueries are an alternate way of returning data from multiple tables.
Subqueries can be used with the following SQL statements along with the comparison
operators like =, <, >, >=, <= etc.
SELECT
INSERT
UPDATE
DELETE
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
Correlated Subquery
A query is called correlated subquery when both the inner query and the outer query
are interdependent.
Non-Correlated Subquery
2) If a subquery is not dependent on the outer query it is called a non-correlated
subquery