4 Advanced SQL Programming
4 Advanced SQL Programming
4 Advanced SQL Programming
The SQL UNION clause/operator is used to combine the results of two or more SELECT statements
without returning any duplicate rows.
To use this UNION clause, each SELECT statement must have
1. (INNER) JOIN: Select records that have matching values in both tables.
2. FULL (OUTER) JOIN: Selects all records that match either left or right table records.
3. LEFT (OUTER) JOIN: Select records from the first (left-most) table with matching right table
records.
4. RIGHT (OUTER) JOIN: Select records from the second (right-most) table with matching left
table records.
Note: All INNER and OUTER keywords are optional.
1. Inner Join
In an inner join, we only select the data which is common in both the tables. (ie,
part 3 here) In order to make it more precise, all the records from both the tables
matching up the condition mentioned with the join are picked in this join.
2. Left Join
In left join, we select all the data from the left table and from the right table only
select the data set which matches up with the condition mentioned with the join
(here area 1+3)
3. Right Join
In right join, we select all the data from the right table and from the left table only
select the data set which matches up with the condition mentioned with the join
(here 3+2)
ALIASES
Aliases are the temporary names given to table or column for the purpose of a particular SQL query. It
is used when name of column or table is used other than their original names, but the modified name is
only temporary.
Aliases are created to make table or column names more readable.
The renaming is just a temporary change and table name does not change in the
original database.
Aliases are useful when table or column names are big or not very readable.
These are preferred when there are more than one table involved in a query.
INDEXES
SUBQUERIES
In SQL a Subquery can be simply defined as a query within another query. In other words we
can say that a Subquery is a query that is embedded in WHERE clause of another SQL query.
Important rules for Subqueries:
You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING clause, FROM
clause.
Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along with
expression operator. It could be equality operator or comparison operator such as =, >, =, <= and
Like operator.
A subquery is a query within another query. The outer query is called as main query and inner
query is called as subquery.
The subquery generally executes first, and its output is used to complete the query condition for the
main or outer query.
Subquery must be enclosed in parentheses.
Subqueries are on the right side of the comparison operator.
ORDER BY command cannot be used in a Subquery. GROUPBY command can be used to
perform same function as ORDER BY command.
Use single-row operators with singlerow Subqueries. Use multiple-row operators with multiple-row
Subqueries.
SQL Subqueries Example :
In this section, you will learn the requirements of using subqueries. We have the following two tables
'student' and 'marks' with common field 'StudentID'.
student marks
Now we want to write a query to identify all students who get better marks than that of the student who's
StudentID is 'V002', but we do not know the marks of 'V002'.
- To solve the problem, we require two queries. One query returns the marks (stored in Total_marks field)
of 'V002' and a second query identifies the students who get better marks than the result of the first query.
First query:
SELECT *
FROM `marks`
Copy
Query result:
Second query:
SELECT a.studentid, a.name, b.total_marks
Copy
Query result:
Above two queries identified students who get the better number than the student who's StudentID is
'V002' (Abhay).
You can combine the above two queries by placing one query inside the other. The subquery (also called
the 'inner query') is the query inside the parentheses. See the following code and query result :
SQL Code:
(SELECT total_marks
FROM marks
Copy
Query result: