4 Advanced SQL Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

UNIONS

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

 The same number of columns selected


 The same number of column expressions
 The same data type and
 Have them in the same order

The basic syntax of UNION can be given with:


SELECT column_list FROM table1_name
UNION SELECT column_list FROM table2_name;

Consider the two tables, customers and employees:

SELECT first_name, last_name FROM employees UNION SELECT first_name,


last_name FROM customers;
JOINS

Four different types of JOINs

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.

Syntax of Inner Join:


SELECT column-names FROM table-name1 INNER JOIN table-name2 ON column-
name1 = column-name2 WHERE condition

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)

Syntax of Left Join:


SELECT column-names FROM table-name1 LEFT JOIN table-name2 ON column-
name1 = column-name2 WHERE condition

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)

Syntax of Right Join:


SELECT column-names FROM table-name1 RIGHT JOIN table-name2 ON column-
name1 = column-name2 WHERE condition
4. Full Join
In full join, all the records from both the tables are merged and selected irrespective
of the condition mentioned with the join having met or not. (here 1+2+3)
Syntax of Full Join:
SELECT column-names FROM table-name1 FULL JOIN table-name2 ON column-
name1 = column-name2 WHERE condition

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`

WHERE studentid = 'V002';

Copy

Query result:

The result of the query is 80.


- Using the result of this query, here we have written another query to identify the students who get better
marks than 80. Here is the query :

Second query:
SELECT a.studentid, a.name, b.total_marks

FROM student a, marks b

WHERE a.studentid = b.studentid

AND b.total_marks >80;

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 a.studentid, a.name, b.total_marks

FROM student a, marks b

WHERE a.studentid = b.studentid AND b.total_marks >

(SELECT total_marks

FROM marks

WHERE studentid = 'V002');

Copy

Query result:

Pictorial Presentation of SQL Subquery:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy