SQL
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.
Customer table
Join Operation
Here, it is noticeable that the join is performed in the WHERE clause. Several operators can be
used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they can all be used
to join tables. However, the most common operator is the equal symbol.
JOINS TYPES
There are different types of joins available in SQL:
INNER JOIN: returns rows when there is a match in both tables.
LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.
Joins, Constraints and Advanced SQL 1.3
RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left
table.
FULL JOIN: returns rows when there is a match in one of the tables.
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.
CARTESIAN JOIN: returns the Cartesian product of the sets of records from the two or more
joined tables.
INNER JOIN
The most commonly used and important of the joins is the INNER JOIN. It is also referred to
as an EQUIJOIN. The INNER JOIN creates a new result table by combining column values of two
tables based upon the join-condition. The query compares each row of the first table with each row of
second table to be joint together to find all pairs of rows which satisfy the join-condition. When the
join-condition is satisfied, column values for each matched pair of rows of A and B are combined into
a result row.
Syntax:
The basic syntax of INNER JOIN is as follows:
SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_filed = table2.common_field;
Now, let us join these two tables using INNER JOIN as follows:
LEFT JOIN
The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the
right table. This means that a left join returns all the values from the left table, plus matched values
from the right table or NULL in case of no matching join condition.
Syntax:
The basic syntax of LEFT JOIN is as follows:
SELECT table1.column1, table2.column2...
FROM table1
LEFT JOIN table2
ON table1.common_filed = table2.common_field;
Consider the following example
SQL> SELECT ID, NAME, AMOUNT, DATE_OF_ORDER
FROM CUSTOMER
LEFT JOIN ORDERS
ON CUSTOMER.ID = ORDERS.CUSTOMER_ID;
RIGHT JOIN
The SQL RIGHT JOIN returns all rows from the right table, even if there are no matches in
the left table. This means that a right join returns all the values from the right table, plus matched
values from the left table or NULL in case of no matching join condition.
Syntax:
The basic syntax of RIGHT JOIN is as follows:
SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2
ON table1.common_filed = table2.common_field;
Consider the following example
SQL> SELECT ID, NAME, AMOUNT, DATE_OF_ORDER
FROM CUSTOMER
RIGHT JOIN ORDERS
Joins, Constraints and Advanced SQL 1.5
ON CUSTOMER.ID = ORDERS.CUSTOMER_ID;
FULL JOIN
The SQL FULL JOIN combines the results of both left and right outer joins. The joined table
will contain all records from both tables, and fill in NULLs for missing matches on either side.
Syntax:
The basic syntax of FULL JOIN is as follows:
SELECT table1.column1, table2.column2...
FROM table1
FULL JOIN table2
ON table1.common_filed = table2.common_field;
Consider the following example
SQL> SELECT ID, NAME, AMOUNT, DATE_OF_ORDER
FROM CUSTOMER
FULL JOIN ORDERS
ON CUSTOMER.ID = ORDERS.CUSTOMER_ID;
CARTESIAN JOIN
The CARTESIAN JOIN or CROSS JOIN returns the Cartesian product of the sets of records
from the two or more joined tables. Thus, it equates to an inner join where the join-condition always
evaluates to True or where the join-condition is absent from the statement.
Syntax:
Joins, Constraints and Advanced SQL 1.7
TYPES OF CONSTRAINTS
The constraints falls under three different categories
1. Domain integrity Constraints
2. Entity integrity Constraints
3. Referential integrity Constraints
constraint will not allow NULL values for the specified column. A NULL is not the same as no data,
rather, it represents unknown data.
Syntax to define a Not Null constraint:
CREATE TABLE <TABLE NAME>(COLUMNNAME DATA TYPE (SIZE)
CONSTRAINT CONSTRAINT_NAME NOT NULL);
Example:
For example, the following SQL creates a new table called CUSTOMERS and adds five
columns, three of which, ID and NAME and AGE, specify not to accept NULLs:
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
KEY (ID)
);
If CUSTOMERS table has already been created, then to add a NOT NULL constraint to
SALARY column, the following statement is used:
ALTER TABLE CUSTOMERS
MODIFY SALARY DECIMAL (18, 2) NOT NULL;
CHECK CONSTRAINTS
The CHECK Constraint enables a condition to check the value being entered into a record. If
the condition evaluates to false, the record violates the constraint and isn't entered into the table.
Syntax to define a Check constraint:
CREATE TABLE <TABLE NAME>(COLUMNNAME DATA TYPE (SIZE) CONSTRAINT
CONSTRAINT_NAME CHECK (CHECK_CONDITION));
Example:
For example, the following SQL creates a new table called CUSTOMERS and adds five columns.
Here, we add a CHECK with AGE column, so that you cannot have any CUSTOMER below 18 years:
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL CHECK (AGE >= 18),
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
KEY (ID)
);
If CUSTOMERS table has already been created, then to add a CHECK constraint to AGE
column, you would write a statement similar to the following:
ALTER TABLE CUSTOMERS
MODIFY AGE INT NOT NULL CHECK (AGE >= 18);
You can also use following syntax, which supports naming the constraint in multiple columns as well:
ALTER TABLE CUSTOMERS
ADD CONSTRAINT myCheckConstraint CHECK (AGE >= 18);
DROP a CHECK Constraint:
To drop a CHECK constraint, use the following SQL.
ALTER TABLE CUSTOMERS
DROP CONSTRAINT myCheckConstraint;
To use ALTER TABLE statement to add a primary key to an already existing table, the
column to which the primary key is to be assigned should have NOT NULL constraint when the table
was first created. If null values are present then primary key cannot be assigned.
For defining a PRIMARY KEY constraint on multiple columns, use the following SQL
statement:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID, NAME)
);
To create a PRIMARY KEY constraint on the "ID" and "NAMES" columns when
CUSTOMERS table already exists, use the following SQL syntax:
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTID PRIMARY KEY (ID, NAME);
Delete Primary Key:
To remove the primary key constraint uses the following syntax:
ALTER TABLE CUSTOMERS DROP PRIMARY KEY;
Example:
Consider the structure of the two tables as follows:
CUSTOMERS table:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
Joins, Constraints and Advanced SQL 1.13