Sql-Ii (DML)
Sql-Ii (DML)
1
Make Sample Table
2
SELECT Statement
SELECT statement is used to select the data from a table.
3
SELECT One Column
Write a query that display EmpNo from table
SELECT EMPNO FROM EMP;
4
SELECT Multiple Columns
Write a query that displays the column EMPNO and
5
SELECT All Columns
Write query that displays all columns from EMP
table.
SELECT *FROM EMP;
6
SELECT DISTINCT Statement
DISTINCT keyword is used to eliminate duplicate rows
7
DISTINCT One column
Write a query to displays all distinct DEPTNO from EMP
table.
SELECT DISTINCT DEPTNO FROM EMP;
8
DISTINCT Multiple columns
Multiple columns may be used with distinct.
9
10
SELECT Statement with
WHERE Clause
WHERE clause is used to retrieve data from a table conditionally. It can
only appears after FROM clause.
Syntax:
EXAMPLE:
NOTE:
Using Quotes: SQL uses single quotes around text values. Most DB
systems also accepts double quotes. Numeric values should not be
enclosed in quotes.
11
Write a query that displays
records of clerks from EMP
table.
12
SELECT Statement with
ORDER BY Clause
The ORDER BY clause is used to sort the rows.
13
Example1: write a query that displays EMP table in
alphabetical order with respect to name.
select *from emp order by ename;
14
15
Example2: write a query that displays ENAME, JOB and SAL
Columns of EMP table in descending order by SAL
Select ename, job, sal from emp order by sal
desc;
16
17
Example3: write a query that displays names and salary of all
employees from EMP. Results should be sorted in ascending
order by DEPTNO and then in descending order by SAL.
18
19
BASIC SQL
• Arithmetic Operators
• Relational Operator
• Logical Operators
• Other Operators (BETWEEN …AND, LIKE,
IS NULL)
20
21
22
Arithmetic Operators
Mathematical operators are the symbols that are used to
23
Example:
Write a query that displays ENAME, annual salary and
COMM from EMP table.
SELECT ENAME, SAL*12, COMM FROM EMP;
24
Output
25
Operator Precedence
The order in w which different types of operators in an
26
Example 1
SELECT ENAME, SAL+250*12 FROM EMP;
27
Example 2
SELECT ENAME, (SAL+250)*12 FROM EMP;
28
Relational operators
The relational operators are used in conditions to
compare one expression with another.
They used in WHERE clause in the following format:
all clerks.
SELECT ENAME,JOB,DEPTNO FROM EMP WHERE
JOB=‘CLERK’;
30
31
Example 2
Write a query that displays all department names with
32
Comparing One Column
with Another
A column can be compared with another column
in the same row as well as with a constant
value.
33
Example 1
Write a query that displays those employees whose
commission is greater than their salary.
34
Logical Operators
Logical operators are used to evaluate compound conditions.
Operators Symbols
AND The AND operator displays a row
if ALL conditions listed are TRUE.
OR The OR operators displays a row
if ANY of the conditions listed are
true.
NOT The NOT operator negates an
expression. 35
Example 1
Write a query that displays all clerks who earned between
1000 and 2000.
SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE
SAL BETWEEN 1000 AND 2000 AND JOB='CLERK';
36
Example 2
Write query that displays all employees who are either
37
38
AND and OR operators can
be combined in logical
expression
AND and OR operators can be combined in logical
expression.
39
Example 3
SELECT EMPNO,ENAME,JOB,SAL FROM EMP
OR JOB='SALESMAN';
40
Example 4
Write query that displays all managers and
salesman with salaries over 1500.
41
42
Other Operators
BETWEEN…. AND
IN Operator
LIKE Operator
IS NULL Operator
43
BETWEEN …. AND
The BETWEEN…. AND operator reterives a range of data
SYNTAX:
44
Examlpe1
Write a query that displays those employees whose salary
is between 1000 and 2000.
45
46
Examlpe2
Write a query that displays those employees whose salary
is not between 1000 and 2000 using NOT aerator.
47
48
IN Operator
The IN Operator is used to test for values in a
specified list. It can be with any data type.
49
Example 1
Write a query that displays all employees who
have one of three MGR numbers.
50
51
Example 2
Write a query that displays the name, job and salary for all
employees whose job is clerk or analyst and their salary is
not equal to 1000, 3000 or 5000.
52
53
LIKE Operator
The LIKE Operator is used to specify a search for a pattern
string.
The % symbol represents any sequence of zero and
more characters
The _ symbol represents any single character
54
Example 1
Write a query that displays all employees whose name
starts with an S.
55
Example 2
Write a query that displays employees names
whose names consist of four characters.
56
Example 3
Write a query that displays the names of all
employees where the third letter of their name is
A.
57
IS NULL Operator
The IS NULL Operator is used to test NULL values.
Example:
Write a query that displays all employs who have a manager
(MGR)
58
59
Exercise
Note: Use following Student table to execute queries.
60
Exercise
Write a query that will display record of all student.
of Students.
Write a query that will retrieve city, age and Name of
students.
61
Exercise
Write a query that will retrieve lname and age of a students. Age must
be display/retrieve addition by 5.
Write a query to show name, age by Addition 10 in age, age by Subtract
either 18 or 20. 62
Exercise Cont..
Write a query that will retrieve data of all students who are enrolled