Clauses:
The clauses are used to retrieve the information from the table.
SQL clause helps us to retrieve a set or bundles of records from
the table. SQL clause helps us to specify a condition on the
columns or the records of a table. Different clauses available in
the Structured Query Language are as follows: WHERE CLAUSE, GROUP
BY CLAUSE, HAVING CLAUSE, ORDER BY CLAUSE.
Joins:
- Join is used to combine the data spread across tables. A join
is performed by the 'where' clause which combines the
specified rows of tables.
- Equi Join :
1.A join based on equalities using the = operator. It
retrieves rows with matching values in both tables.
2.Syntax: SELECT columns FROM table1, table2
WHERE table1.column1 = table2.column2;
3.Example: SELECT emp. empno, emp.ename, emp.deptno,
dept.deptno, dept.loc
FROM emp, dept
WHERE emp.deptno = dept.deptno;
4.Output: Rows with matching deptno values from both emp
and dept tables.
- Non Equi Join
1.A join using relational operators other than = (e.g., <, >,
<=, >=, !=).
2.Syntax: SELECT columns FROM table1, table2
WHERE table1.column1 < table2.column2;
3.Example: SELECT e.ename, e.salary, s.grade
FROM emp e, salgrade s
WHERE e.salary BETWEEN s.losal AND s.hisal;
4.Output: Rows from emp and salgrade where emp.salary falls
between salgrade.losal and salgrade.hisal
- Self Join:
1.A join where a table is joined with itself. It compares rows
within the same table.
2.Syntax: SELECT a.column1, b.column2 FROM table a, table b
WHERE a.column = b.column;
3.Example: SELECT worker.ename "employee", manager.ename
"manager" FROM emp worker, emp manager WHERE
worker.mgr = manager.empno;
4.Output: Rows where each worker's manager is also listed in
the emp table.
- Left Outer Join:
1.Returns all records from the left table and matched records
from the right table. Returns NULL for non matching rows from
the right table.
2.Syntax: SELECT columns FROM table1 LEFT OUTER JOIN table2 ON
table1.column = table2.column;
3.Example: SELECT EMP.emp_id, EMP.name, DEPT.dept_name FROM EMP
LEFT OUTER JOIN DEPT ON EMP.dept_id = DEPT.dept_id;
4.Output: All records from EMP table and matching records from
DEPT. Non matching DEPT rows are NULL.
- Right outer join:
1.Returns all records from the right table and matched records
from the left table. Returns NULL for non matching rows from
the left table.
2.Syntax: SELECT columns FROM table1 RIGHT OUTER JOIN table2 ON
table1.column = table2.column;
3.Example: SELECT EMP.emp_id, EMP.name, DEPT.dept_name FROM EMP
RIGHT OUTER JOIN DEPT ON EMP.dept_id = DEPT.dept_id;
4.Output: All records from DEPT table and matching records from
EMP. Non matching EMP rows are NULL.
Set Operators:
1.Set operators combine the results of two component queries
into a single result.
2.Queries containing set operators are called compound queries.
3.Set operators in SQL are represented with following special
keywords as:
4.Union, Union all, intersection & minus. Consider data from
two tables emp and employee as
1) Union:
1.The Union of two or more sets contains all elements,
which are present in either or both. Union works as or.
2.Syntax: SELECT * FROM tablename1 UNION SELECT * FROM
tablename2;
3.Example: SELECT * FROM emp UNION SELECT * FROM dept;
2) Union all:
1.The Union of 2 or more sets contains all elements, which
are present in both, including duplicates.
2.Syntax: SELECT * FROM tablename1 UNION ALL SELECT * FROM
tablename2;
3.Example: SELECT * FROM emp UNION ALL SELECT * FROM dept;
3) Intersection:
1.The intersection of two sets includes elements which are
present in both.
2.Syntax: SELECT * FROM tablename1 INTERSECT SELECT * FROM
tablename2;
3.Example: SELECT * FROM emp INTERSECT SELECT * FROM dept;
4) Minus:
1.The minus of two sets includes elements from set1 minus
elements of set2.
2.Syntax: SELECT * FROM tablename1 MINUS SELECT * FROM
tablename2;
3.Example: SELECT * FROM emp MINUS SELECT * FROM dept;
Arithmetic Operators:
1.The arithmetic operators are used on the data stored in the
tables.
2.We can use these operators with the SELECT statement in SQL.
3.We can also use the WHERE clause in the SELECT statement for
performing operations on particular rows.
4.The arithmetic operators are used between two numeric
operands for performing addition, subtraction, multiplication
and division operations