SQL Joins
SQL Joins
The SQL Joins clause is used to combine records from two or more tables in a database. A
JOIN is a means for combining fields from two tables by using values common to each.
INNER JOIN
The most frequently used and important of the joins is the INNER JOIN. They are also referred
to as an EQUIJOIN..
LEFT JOIN
The SQL Left Join returns all the values from the left table, plus matched values from the right
table or NULL in case of no matching.
FULL JOIN
The SQL FULL JOIN combines the results of both left and right outer joins.
CARTESIAN JOIN
● The CARTESIAN JOIN or CROSS JOIN returns the cartesian product of the sets of
records from the two or more joined tables.
● It produces a result set which is the number of rows in the first table multiplied by the
number of rows in the second table if no WHERE clause is used along with CROSS
JOIN.
● If WHERE clause is used with CROSS JOIN, it functions like an INNER JOIN.
SELF JOIN
The SQL SELF JOIN is used to join a table to itself, as if the table were two tables, temporarily
renaming at least one table in the SQL statement.
UNION CLAUSE
The SQL UNION clause/operator is used to combine the results of two or more SELECT
statements without returning any duplicate rows.
To use UNION, each SELECT must have the same number of columns selected, the same
number of column expressions, the same data type, and have them in the same order but they
do not have to be the same length.
UNION
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
The same rules that apply to UNION apply to the UNION ALL operator.
UNION ALL
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
INTERSECT CLAUSE
The SQL INTERSECT clause/operator is used to combine two SELECT statements, but returns
rows only from the first SELECT statement that are identical to a row in the second SELECT
statement. This means INTERSECT returns only common rows returned by the two SELECT
statements.
The same rules that apply to UNION apply to the INTERSECT operator.
INTERSECT
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
The same rules that apply to UNION apply to the EXCEPT operator.
EXCEPT
ELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
Indexes
Indexes are special lookup tables that the database search engine can use to speed up data
retrieval. An index helps speed up SELECT queries and WHERE clauses, but it slows down
data input, with UPDATE and INSERT statements. Indexes can be created or
dropped with no effect on the data.
Composite Indexes:
CREATE INDEX index_EID on table_EID (column1, column2);
Implicit Indexes: Implicit indexes are indexes that are automatically created by the database
server when an object is created. Indexes are automatically created for primary key constraints
and unique constraints.
A view can contain all rows of a table or select rows from a table. A view can be created from
one or many tables which depends on the written SQL query to create a view.
Views which are kind of virtual tables, allow users to do the following:
Structure data in a way that users or classes of users find natural or intuitive.
Restrict access to the data such that a user can see and (sometimes) modify exactly what they
need and no more.
Summarize data from various tables which can be used to generate reports.
VIEWS
CREATE VIEW view_EID AS
(SELECT column1, column2.....
FROM table_EID
WHERE [condition]
);
Updating a Views
A view can be updated under certain conditions:
Dropping Views
DROP VIEW view_EID;
The WHERE clause places conditions on the selected columns, whereas the HAVING clause
places conditions on groups created by the GROUP BY clause
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
The HAVING clause must follow the GROUP BY clause in a query and must
also precede the ORDER BY clause if used.
The following is the syntax of the SELECT statement, including the HAVING
clause:
SQL FUNCTIONS
● COUNT Function - The SQL Server COUNT aggregate function is used to count the
number of rows in a database table.
● MAX Function - The SQL Server MAX aggregate function allows the user to select the
highest (maximum) value for a certain column.
● MIN Function - The SQL Server MIN aggregate function allows the user to select the
lowest (minimum) value for a certain column.
● AVG Function - The SQL Server AVG aggregate function selects the average value for
a certain table column.
● SUM Function - The SQL Server SUM aggregate function allows selecting the total for a
numeric column.
● SQRT Function - This is used to generate a square root of a given number.
● RAND Function - This is used to generate a random number using SQL command.
● CONCAT Function - This is used to concatenate multiple parameters to a single
parameter.