0% found this document useful (0 votes)
2 views27 pages

Relations SQL

The document explains various SQL clauses and operations including ORDER BY, GROUP BY, HAVING, and different types of JOINs such as Cartesian Product, Equi Join, Non-Equi Join, and Natural Join. It also covers the UNION, MINUS, and INTERSECT operators, detailing their usage and limitations in MySQL. Additionally, it notes that MySQL does not support MINUS and INTERSECT operators directly, suggesting alternative methods to achieve similar results.

Uploaded by

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

Relations SQL

The document explains various SQL clauses and operations including ORDER BY, GROUP BY, HAVING, and different types of JOINs such as Cartesian Product, Equi Join, Non-Equi Join, and Natural Join. It also covers the UNION, MINUS, and INTERSECT operators, detailing their usage and limitations in MySQL. Additionally, it notes that MySQL does not support MINUS and INTERSECT operators directly, suggesting alternative methods to achieve similar results.

Uploaded by

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

RELATIONAL

SQL

By:
Neeraj Namdev
PGT(CS)
JNV Pathnamthitta
ORDER BY CLAUSE

 ORDER BY clause is used to display the result of a


query in a specific order(sorted order).

 The sorting can be done in ascending or in


descending order. It should be kept in mind that the
actual data in the database is not sorted but only the
results of the query are displayed in sorted order.

 Note:- If order is not specifies that by default the sorting will be performed in
ascending order.
GROUP BY
 The GROUP BY clause can be used in a SELECT statement to
collect data across multiple records and group the results by one
or more columns.
EXAMPLE :
HAVING CLAUSE
 The HAVING clause is used in combination with the GROUP BY
clause. It can be used in a SELECT statement to filter the records
that a GROUP BY returns.
JOINS
A JOIN is a query through which we can extract
queries from two or more tables. It means, it
combines rows from two or more tables. Rows in
one table can be joined to rows in another table
according to common values existing in
corresponding columns.
TYPES OF JOIN

There are various types of joins:


 Cartesian Product
 Equi Join

 Non-Equi Join
 Natural Join
CARTESIAN PRODUCT
OR
CROSS JOIN

The CROSS JOIN or CARTESIAN JOIN is used


to produce the Cartesian product of two tables.
The Cartesian product is a basic type of join that
matches each row from one table to every row
from another table.
TABLES- BOOK3 AND BOOK1
FOR PERFORMING CROSS JOIN
CROSS JOIN
TABLES FOR
EQUIJOIN, NON EQUI JOIN & NATURAL JOIN
EQUI JOIN
In an EQUI-JOIN operation, the values in the
columns are being joined and compared for equality.
All the columns in the tables being joined are
included in the results.
NON-EQUI JOIN

Non equi join is a query that specifies some


relationship other than equality between the
columns. It means that records are joined on the
condition other than Equal operator
(<,>,<>,>=,<=) for joining column.
NON EQUI JOIN
NATURAL JOIN
The join in which one of the identical columns exist,
is called Natural Join. The natural join is much
similar to Equi-Join, records are joined on the
equality condition of joining column except that the
common column appears one time.
UNION
The UNION operator is used to combine the result-set
of two or more SELECT statements. Notice that each
SELECT statement within the UNION must have the
same number of columns. The columns must also have
similar data types. Also, the columns in each SELECT
statement must be in the same order.

 Note: The UNION operator selects only distinct values


by default. To allow duplicate values, use the ALL
keyword with UNION.
UNION OF TWO QUERY
TABLES FOR
UNION, INTERSECT & MINUS
EXAMPLE :
UNION ALL
MINUS

 MINUS is a little bit different. Let’s say we want


to see only book titles that are not also movie
titles. We need to “minus” everything from
the BOOK1 table that is also in
the BOOK2 table. The MINUS set operator is
designed for this type of task.
MINUS OF TWO QUERY
MINUS OPERATION IN MYSQL
MySqL does not support Minus operator so here we use left join to achieve
minus operation in MySQL.
INTERSECTION

An INTERSECT query returns the intersection of


2 or more data sets. If a record exists in both data
sets, it will be included in the INTERSECT
results. However, if a record exists in one data set
and not in the other, it will be omitted from the
INTERSECT results.
INTERSECTION
INTERSECTION OPERATION IN MYSQL

MySqL does not support Intersection operator so here we use join operator
to achieve minus operation in MySQL.

You might also like