BASIC SQL
1
Make Sample Table
2
SELECT Statement
SELECT statement is used to select the data from a table.
It displays result in a tabular form
Syntax:
SELECT column_name(s) FROM table_name;
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
ENAME from EMP table.
SELECT EMPNO, ENAME FROM EMP;
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
from the result of a SELECT statement. If DISTINCT is
not used, all rows are returned including duplicates.
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.
Write a query that displays distinct DEPTNO and JOB
from EMP table.
SELECT DISTINCT DEPTNO, JOB FROM EMP;
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:
SELECT Column(s) FROM table WHERE condition;
EXAMPLE:
Write a query that displays records of clerks from EMP table.
SELECT *FROM EMP WHERE JOB=‘CLERK’;
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.
The process of arranging data or records is called sorting.
A sort can be ascending or descending.
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.
select ename, deptno, sal from emp order by
deptno, sal desc;
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
perform air thematic operations on data.
An operator is a symbol that perform some operation.
Arithmetic expressions may contain column names,
constant numeric values and the arithmetic operators.
Operators Symbols
+ Add
- Subtract
* Multiply
/ Divide
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
expression are evaluated is known as operator precedence.
It is also known as hierarchy of operations.
Multiplication and division operators are performed before
addition and subtraction.
When an expression contains operators of the same order,
the expression is evaluated from left to right.
Use parenthesis to force a specific order of evaluation.
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:
WHERE exp operator value
Operators Symbols
> Greater than
< Less than
= Equal to
>= Greater than or equal
to
<= Less than or equal to
!= Not equal to
29
Example 1
Write a query that displays names, job and department of
all clerks.
SELECT ENAME,JOB,DEPTNO FROM EMP WHERE
JOB=‘CLERK’;
30
31
Example 2
Write a query that displays all department names with
department number greater than 20.
SELECT DNAME, DEPTNO FROM DEPT WHERE
DEPTNO>20;
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.
SELECT ENAME, SAL,COMM FROM EMP WHERE
COMM>SAL;
34
Logical Operators
Logical operators are used to evaluate compound conditions.
The logical operators are AND, OR and NOT. AND and OR
are used to give two or more conditions in a WHERE clause.
NOT negates the search condition.
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
clerk and/or all employees who earn between 1000 and
2000.
SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE
SAL BETWEEN 1000 AND 2000 OR JOB='CLERK';
37
38
AND and OR operators can
be combined in logical
expression
AND and OR operators can be combined in logical
expression.
If AND and OR appear in same WHERE clause, all ANDs
are performed first then all ORs are performed.
It means that AND has a higher precedence than OR.
39
Example 3
SELECT EMPNO,ENAME,JOB,SAL FROM EMP
WHERE SAL>1500 AND JOB='MANAGER'
OR JOB='SALESMAN';
40
Example 4
Write query that displays all managers and
salesman with salaries over 1500.
SELECT EMPNO,ENAME,JOB,SAL FROM EMP
WHERE SAL>1500 AND (JOB='MANAGER'
OR JOB='SALESMAN');
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
between two values.
The values can be numeric, text or dates.
SYNTAX:
SELECT column_name FROM table_name WHERE
column_name BETWEEN value1 AND value2;
44
Examlpe1
Write a query that displays those employees whose salary
is between 1000 and 2000.
SELECT ENAME, SAL FROM EMP
WHERE SAL 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.
SELECT ENAME, SAL FROM EMP
WHERE SAL NOT BETWEEN 1000 AND 2000;
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.
SELECT EMPNO, ENAME, JOB, SAL, MGR FROM EMP
WHERE MGR IN (8902, 7566, 7788);
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.
SELECT EMPNO, ENAME, JOB, SAL FROM EMP
WHERE JOB IN ('CLERK', 'ANALYST')
AND SAL NOT IN(1000, 3000, 5000);
52
53
LIKE Operator
The LIKE Operator is used to specify a search for a pattern
in a column. The character pattern matching operation
may be referred to as wild-cards.
The two symbols can be used to construct the search
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.
SELECT ENAME FROM EMP
WHERE ENAME LIKE 'S%';
55
Example 2
Write a query that displays employees names
whose names consist of four characters.
SELECT ENAME FROM EMP
WHERE ENAME LIKE '____';
56
Example 3
Write a query that displays the names of all
employees where the third letter of their name is
A.
SELECT ENAME FROM EMP
WHERE ENAME LIKE '__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)
SELECT ENAME, MGR FROM EMP
WHERE MGR IS NOT NULL;
58
59
Exercise
Note: Use following Student table to execute queries.
60
Exercise
Write a query that will display record of all student.
Write a query that will display only age of student.
Write a query that will display Roll_No and Name of student.
Write a query that will retrieve Discipline, Section and CGPA
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
5 in age, age by Multiply 2 in age and age by division 2 in age of
Students.
Write a query that will display all students whose cgpa is equal to “1.4”
and Section is equal to “A”.
Write a query that will display name as a Student Name of all students
whose age is equal 23 and order by name descending.
Write a query that will retrieve data of all student whose Discipline is
equal to “BSCS” and city must be “Rwp” or “Multan”.
Write a query that will retrieve the cgpa from students where age is
either 18 or 20. 62
Exercise Cont..
Write a query that will retrieve data of all students who are enrolled
in Degree “BSCS” and Cgpa greater than 3.3.
Write a Sql Statement to retrieve all students where cgpa not equal to
2.4 and 3.4
Write a query that will retrieve Name as “Full Name” and degree,
Section as a “Discipline Section” of Students who’s Cgpa greater than
equal to 3.2 order by section descending.
Write a query to display the name (fName, lName) and age for all
students whose age is not in the range of 19 and 21
Write a query to display the full name (first and last name), and city
for all students whose section is not A.
Write a query to select all record from students where last name in
63