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

SQL Notes Chapter 8

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)
5 views

SQL Notes Chapter 8

Uploaded by

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

SELECTION

The retrieval of data from the table by selecting both columns and records is known as
Selection.
Syntax:
SELECT */[DISTINCT] COL_NAME/EXPRESSION [ALIAS]
FROM TABLE_NAME
WHERE <filter condition>;
Order of execution
1.FROM
2.WHERE – Row by row
3.SELECT – Row by row
 WHERE clause is used to filter the records.
 WHERE clause executes row by row.
 WHERE clause executes after the execution of FROM clause.
 For WHERE clause we have to pass filter condition as an argument.
 For WHERE clause we can pass multiple filter conditions by using logical
operators.
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

WAQTD Employee names and their dept no’s where the employees should be
working in dept no 20.
SELECT ENAME, DEPTNO
FROM EMP
WHERE DEPTNO=20;
Execution

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

Output of FROM Clause


EMPNO ENAME SAL DEPTNO
1 ALLEN 2000 20
4 SCOTT 2000 20
Output of WHERE Clause
ENAME DEPTNO
ALLEN 20
SCOTT 20
Output of SELECT Clause
2.WAQTD Details of an employee whose name is SMITH.
SELECT *
FROM EMP
WHERE ENAME=’SMITH’;
3.WAQTD Employee names and their salary where the employees should be
earning more than 1250.
SELECT ENAME, SAL
FROM EMP
WHERE SAL>1250;
Questions on WHERE clause
1.WAQTD THE ANNUAL SALARY OF THE EMPLOYEES WHOSE NAME IS
ALLEN.
2.WAQTD NAMES OF THE EMPLOYEES WORKING AS CLERK.
3.WAQTD SALARY OF THE EMPLOYEES WORKING AS SALESMAN.
4.WAQTD DETAILS OF THE EMPLOYEES WHO EARNS MORE THAN 2000.
5.WAQTD DETAILS OF THE EMPLOYEES WHOSE NAME IS JONES.
6.WAQTD DETAILS OF THE EMPLOYEES WHO HAS HIRED AFTER 01-JAN-
81.
7.WAQTD NAME AND SALARY ALONG WITH THEIR ANNUAL SALARY IF
THE ANNUAL SALARY IS MORE THAN 12000.
8.WAQTD EMPNO OF THE EMPLOYEE WHO ARE WORKING IN DEPTNO 30.
9.WAQTD NAME AND HIREDATE IF THEY ARE HIRED BEFORE 1981.
10.WAQTD DETAILS OF THE EMPLOYEES WORKING AS MANAGER.
OPERATORS IN SQL
1. Arithmetic operator (+, -, *, /, ……)
2. Relational operator (<, <=, >, >=)
3. Comparison operator (=, !=)
4. Concatenation operator (||)
5. Logical operator (AND, OR, NOT)
6. Special operator (IN, NOT IN, BETWEEN, NOT BETWEEN, NOT
BETWEEN, IS, IS NOT, LIKE, NOT LIKE)
7. Subquery operator (ALL, ANY, EXIST, NOT EXIST)
CONCATENATION OPERATOR
CONCATENATION operator is used to join the given two strings or columns.
Example:
1.WAQTD to join employee names and salary column.
SELECT ENAME||SAL
FROM EMP;
Example format 1:
EMPLOYEE SMITH SALARY IS 800
SELECT ‘EMPLOYEE’||’ ‘||ENAME||’ ‘||’SALARY IS’||SAL
FROM EMP;
Example format 2:
THE EMPLOYEE SMITH DEPARTMENT NO IS 20.
SELECT ‘THE EMPLOYEE’||’ ‘||ENAME||’ ‘||’DEPARTMENT NUMBER IS’||’
‘||DEPTNO
FROM EMP;
LOGICAL OPERATOR
1. AND: AND operator returns true only if all the conditions are true. AND
operator is used between the conditions.

Input 1
Output
Input 2

TRUTH TABLE
INPUT 1 INPUT 2 OUTPUT
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE

AND Operator follows Binary multiplication.


INPUT 1 INPUT 2 OUTPUT
0 0 0
0 1 0
1 0 0
1 1 1

2. OR: OR operator returns true even if any of the conditions any one of the
conditions is true. OR operator is used between the conditions.

Input 1
Output
Input 2

TRUTH TABLE
INPUT 1 INPUT 2 OUTPUT
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
OR operator follows Binary addition.
INPUT 1 INPUT 2 OUTPUT
0 0 0
0 1 1
1 0 1
1 1 1

3. NOT: NOT operator is used as negation. NOT operator will accept only one
input. If we pass true, it will return false Similarly if we pass false, it will return
true.

Input Output

TRUEFALSE
FALSETRUE
01
10

You might also like