SQL Theory
SQL Theory
Query:
o Query is a type of SQL commands which accepts tables (relations), columns
(fields or attributes) and conditions or specifications if any and display the
output by means of a temporary table which consists of data represented
through fields and records.
Order of execution of a query:
o Step 1: Identify table(s) with FROM clause
o Step 2: Filter records using WHERE clause
o Step 3: Form group if any using GROUP BY clause
o Step 4: Filter groups using HAVING clause only if GROUP BY is used
o Step 5: Arrange the output records in ascending or descending order using
ORDER BY
o Step 6: Display the fields mentioned in SELECT clause.
JOINS:
o A relational database consists of multiple related tables linking together
using common columns, which are known as foreign key columns.
o It is used retrieve data from multiple tables.
Types of Join:
o Cartesian Product or Cross join:
The cross join makes a Cartesian product of rows from the joined
tables.
The cross join combines each row from the first table with every row
from the right table to make the result set.
If Table1 has degree d1 and cardinality c1 and table2 has degree d2
and cardinality c2, their Cartesian Product has degree d=d1+d2 and
cardinality c=c1*c2;
Syntax: SELECT * FROM table1, table2
o Equi Join:
Equi join is a join operation which works on the equality condition of
values in two
columns from two tables having similar data type.
o Natural Join:
A natural join is a type of join operation that creates an implicit join by
combining tables based on columns with the same name and data
type.
It makes the SELECT query simpler with minimal use of conditions.
There is no need to specify the name of common column in the
SELECT statement
Common column is present only once in the output.
Syntax: SELECT * FROM Table1 NATURAL JOIN TABLE2;
Difference between Equi-Join vs Natural Join:
Equi-Join Natural Join
Join performed on equality Join is performed on column
of value of the columns having common name.
Where clause is used to There is no need to use
specify the condition where clause
Both columns from tables Common column is
are displayed in the result. displayed only once
Difference between CHAR & VARCHAR :
Char varchar
Fixed length string Variable length string
Fast, no memory allocation Slow, as it take size
every time according to data so every
time memory allocation is
done
It takes more memory It takes less space
GROUP BY:
o GROUP BY clause is used if statistical records of a table are to be displayed
based on a field. Once the group is formed individual records cannot be
accessed in that query. Several clusters or groups are formed based on the
number of different values in the GROUP BY column present in the table.
o For example, if GROUP BY is applied on TYPE field of ITEM table 3 groups are
formed – Crops have 2 records, Leaves and Pulses have one record each
HAVING:
o It is a conditional statement used along with group by clause only. It
compares the values with the outcome of aggregate functions belonging to
each group already formed by GROUP BY clause.
Difference between WHERE and HAVING: