Professional Documents
Culture Documents
STUDENT
Table 1
SQL Commands
• SQL commands are instructions. It is used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
• SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE EMPLOYEE;
c. ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition;
d. TRUNCATE: It is used to delete all the rows from the table and free the space containing
the table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEE;
b. UPDATE: This command is used to update or modify the value of a column in the table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHE
RE CONDITION]
For example:
UPDATE students SET User_Name = 'Sonoo' WHERE Student_Id = '3'
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back
the entire transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
a. SELECT: This is the same as the projection operation of relational algebra. It is used to
select the attribute based on the condition described by WHERE clause.
Syntax:
SELECT expressions FROM TABLES WHERE conditions;
For example:
SELECT emp_name FROM employee WHERE age > 20;
JOINS:
A join is used to combine rows from multiple tables. Join is performed whenever two
or more tables is listed in the FROM clause of an SQL statement. The sql JOIN clause
is used whenever we have to select data from 2 or more tables. The sql JOIN clause is
used to retrieve data from 2 or more tables joined by common fields. The most
common scenario is a primary key from one of the tables matches a foreign key in
second table.
A join is a mechanism that allows the tables to be linked together. The rows retrieved
after joining the two tables based on a condition in which one table act as a primary
table and other act as a foreign table. The columns in the first table which act as a
foreign key must match to the column in the second table which is defined as a
primary key referenced by foreign key.
Note:
• Join = Cross product + some condition
• Common Attributes
SQL Aliases
• SQL aliases are used to give a table, or a column in a table, a temporary name.
• Aliases are often used to make column names more readable.
• An alias only exists for the duration of that query.
• An alias is created with the AS keyword.
Alias Column Syntax
1.NATURAL JOIN
Natural join is an SQL join operation that creates join on the base of the
common columns in the tables. To perform natural join there must be one common
attribute(Column) between two tables. Natural join will retrieve from multiple
relations. A NATURAL JOIN can be an INNER join, a LEFT OUTER join, or a RIGHT
OUTER join. The default is INNER join.
Features of Natural Join:
1. It will perform the Cartesian product.
2. It finds consistent tuples and deletes inconsistent tuples.
3. Then it deletes the duplicate attributes..
Natural Join: Guidelines
- The associated tables have one or more pairs of identically named columns.
- The columns must be the same data type.
- Don’t use ON clause in a natural join.
Syntax:
Output:
Rose
Mary
John
Jack
EQUI JOIN :
EQUI JOIN creates a JOIN for equality or matching column(s) values of the relative
tables. EQUI JOIN also create JOIN by using JOIN with ON and then providing the
names of the columns with their relative tables to check equality using equal sign
(=).
Syntax :
SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;
or
Syntax :
SELECT column_list
FROM table1
JOIN table2
[ON (join_condition)]
Example –
2. Self Join
The 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. A self join uses
the inner join or left join clause. Because the query that uses the self join references
the same table, the table alias is used to assign different names to the same table
within the query.
Syntax:
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
T1 and T2 are different table aliases for the same table.
Outer Join:
• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table
• FULL (OUTER) JOIN: Returns all records when there is a match in either left or
right table
LEFT JOIN
The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from the right
side, if there is no match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Employee:
Department:
Output:
The RIGHT JOIN keyword returns all records from the right table (table2), and the
matching records from the left table (table1). The result is 0 records from the left
side, if there is no match.
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Employee:
Department:
Output:
The FULL OUTER JOIN keyword returns all records when there is a match in left
(table1) or right (table2) table records.
Tip: FULL OUTER JOIN and FULL JOIN are the same.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Note: FULL OUTER JOIN can potentially return very large result-sets!
Employee:
Output: