0% found this document useful (0 votes)
11 views

SQL Notes Chapter 6 & 7

Uploaded by

devap9407
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Notes Chapter 6 & 7

Uploaded by

devap9407
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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
1.PROJECTION
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.
1.FROM clause starts the execution.
2.For FROM Clause we have to pass table name as an argument.
3.FROM 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.
4.After the successful execution of from clause next SELECT clause starts the
execution.
5.For select clause we have to pass *(ASTERISK), COLUMN_NAME or Expression
as an argument.
6.SELECT Clause is used to select the data and display it.
7.SELECT 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
1.WAQTD Display details of employees.
SELECT *
FROM EMP;
2.WAQTD Display details of departments.
SELECT *
FROM DEPT;
3.WAQTD Names of all the employees.
SELECT ENAME
FROM EMP;
4.WAQTD Employee names and their salaries.
SELECT ENAME, SAL
FROM EMP;
5.WAQTD Salaries, Hire date, Deptno, Commission and Names of all the
employees.
SELECT SAL, HIREDATE, DEPTNO, COMM, ENAME
FROM EMP;
6.WAQTD 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. *
7.WAQTD Details of all the employees along with the employee numbers.
SELECT EMP.*, EMPNO
FROM EMP;
8.WAQTD 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
1.WAQTD 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.
1.WAQTD 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;
2.WAQTD 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

1.WAQTD different dept no’s present in employee table.


SELECT DISTINCT DEPTNO
FROM EMP;
DEPTNO
10
20
30

2.WAQTD 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