0% found this document useful (0 votes)
30 views5 pages

SQL Notes Chapter 6 & 7

Uploaded by

devap9407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views5 pages

SQL Notes Chapter 6 & 7

Uploaded by

devap9407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4.

JOINS
DATA QUERY LANGUAGE
In this language, we learn how to fetch the data from the database.
Statements in DQL
1. SELECT
2. PROJECTION
3. SELECTION
4. JOINS
[Link]
PROJECTION is used to retrieve the data from the table by selecting only columns.
Syntax:
SELECT */[DISTINCT] COL_NAME/EXPRESSION [ALIAS]
FROM TABLE_NAME;
Order of execution
1. FROM Clause
2. SELECT Clause
Note: SQL is not a case sensitive Language but the literals are case sensitive.
[Link] clause starts the execution.
[Link] FROM Clause we have to pass table name as an argument.
[Link] clause will go to the database and search for the given table, if the table is
present in the database then the table will be kept under execution else FROM clause
will stop the execution and gives you an error message.
[Link] the successful execution of from clause next SELECT clause starts the
execution.
[Link] select clause we have to pass *(ASTERISK), COLUMN_NAME or Expression
as an argument.
[Link] Clause is used to select the data and display it.
[Link] Clause is responsible for the result table.
ASTERISK (*):
It is used to select all the columns present in the table. We can also say that it is used
to display the entire table.
SEMI COLON:
It is used for end of statement.
Examples
[Link] Display details of employees.
SELECT *
FROM EMP;
[Link] Display details of departments.
SELECT *
FROM DEPT;
[Link] Names of all the employees.
SELECT ENAME
FROM EMP;
[Link] Employee names and their salaries.
SELECT ENAME, SAL
FROM EMP;
[Link] Salaries, Hire date, Deptno, Commission and Names of all the
employees.
SELECT SAL, HIREDATE, DEPTNO, COMM, ENAME
FROM EMP;
[Link] Dept Names, Locations.
SELECT DNAME, LOC
FROM DEPT;
Note: Whenever we want to display any columns or expression along with the table in
the result we have to use the following syntax in the place of asterisk (*).
Syntax:
TABLE_NAME. *
[Link] Details of all the employees along with the employee numbers.
SELECT EMP.*, EMPNO
FROM EMP;
[Link] Details of the departments along with DEPT Names and Locations.
SELECT DEPT.*, DNAME, LOC
FROM DEPT;
EXPRESSION
The statement which gives the result is known as expression.

Expression

Operator(+,-
Operand
,*,...)

Column Literals
name (value/data)

Character Number
Date literals
literal literal

Note: CHARACTER AND DATE LITERALS MUST BE ENCLOSED WITHIN


SINGLE QUOTES (‘’)
Example: Operator
SAL*12

Column name Literal

Operand
s
[Link] annual salaries employees.
SELECT SAL*12
FROM EMP;
ALIAS
Alias is an alternative name which is given for a column/expression.
Alias name can be used to with or without using the keyword AS.
Alias name can be a single word or a string, String should be enclosed within (“ ”)
double quotes or enclosed within double quotes.
[Link] Annual salary of employees (By using ALIAS name).
SELECT SAL*12 AS ANNUAL_SALARY
FROM EMP;
OR
SELECT SAL*12 “ANNUAL_SALARY”
FROM EMP;
OR
SELECT SAL*12 “ANNUAL_SALARY”
FROM EMP;
[Link] to give ALIAS name to Sal column.
SELECT SAL SALARY
FROM EMP;
DISTINCT Clause
Distinct clause is used to remove the duplicate records present in the table.
Distinct clause has to be used as the first argument in select clause.
Whenever we pass multiple arguments to distinct clause it always removes the
combination of columns that are duplicated.
Example 1:
EMPNO ENAME SAL DEPTNO
1 ALLEN 2000 20
2 BLAKE 3000 10
3 SMITH 9000 30
4 SCOTT 2000 20
5 JAMES 4000 30
6 BLAKE 3000 10

[Link] different dept no’s present in employee table.


SELECT DISTINCT DEPTNO
FROM EMP;
DEPTNO
10
20
30

[Link] Different salaries and dept no’s present in employee table.


SELECT DISTINCT SAL, DEPTNO
FROM EMP;
SAL DEPTNO
2000 20
3000 10
9000 30
4000 30

You might also like