SQL Basics Recap
SQL Basics Recap
Structured Query
Language Recap
Structured Query Language Basics
Select Statement
• Declarative
Language
Select Statement
• Assumption is
tables are created
and data is
populated
Structured Query Language Basics
Implementing
Select Statement
Structured Query Language Basics
Select Statement
• Assumption is
tables are created
and data is
populated
Structured Query Language Basics
SQL and where
clause
Implementing
Where Clause
Structured Query Language Basics
• Table Name: Emp
• Question # 1:
• Write a query to find out list of all those
employee name who are earning more than
2500 but less than 5000.
• Question # 2:
• Write a query to find out all those employees
who are working in Dept # 20 with
designation of Analyst but not earning more
than 2000 and was hired at least 30 years
ago.
Structured Query Language Basics
• Table Name: Emp
• Question # 1:
• Write a query to find out list of all those
employee name who are earning more than
2500 but less than 5000.
• Solution:
• Select ename from emp where sal>2500 and
sal < 5000
Structured Query Language Basics
• Table Name: Emp
• Question # 1:
• Write a query to find out all those employees
who are working in Dept # 20 with
designation of Analyst but not earning more
than 2000 and was hired at least 30 years
ago.
• Solution:
Implementing
Where Clause
• In where clause we
have recap logical
operators and
comparison
operators
Structured Query Language Basics
Wild Cards
Wild Cards
• %: Zero or more
characters
• - : Exactly one
character
Structured Query Language Basics
Implementing
Wild Cards - I
• Solution:
Implementing
Wild Cards - II
Structured Query Language Basics
• Write a query to display all information
about all those employees who are having ER
in the job with at least three character in job
and should be earning at least 2500 but at
most 5000 and should be with company for at
most 15 years
• Single row
function operator
in single row
• SELECT SUBSTR('ABCDEFG',3,4)
"Substring" FROM DUAL;
Structured Query Language Basics
Group Functions
• Group functions
operate on
multiple rows
• Group functions
cannot be used in
where clause
Structured Query Language Basics
Implementing
Group Functions-I
Structured Query Language Basics
• Write a query to display sum, minumum,
maximum and average salaries which
company is paying to its employees
• Solution:
Implementing
Group Functions-II
Structured Query Language Basics
• Write a query to display sum, minumum,
maximum and average salaries which
company is paying to its employees but
employees from Dept# 20 should not be
shown and average salaries should be less
than 1500
• Solution:
• Group by clause
group together
similar row
together to form
group
• Groups functions
are used with
Group by clause
Structured Query Language Basics
Group By Clause
• After Select
statement only
those columns can
be displayed which
are written after
Group-by clause
Structured Query Language Basics
Implementing
Group By Clause
Structured Query Language Basics
• What is the total salary paid by each
department
• to its employees.
• Steps to Solution:
• Solution
Implementing
Group By Clause-II
Structured Query Language Basics
What is average and maximum salary paid to
each Job who are reporting to MGR 7839
and all the emlpoyees should have no
occurrence of K in their ename
Solution -1
Solution -2:
To restrict groups
having clause is
used.
Equivalent to
Where clause
except having is
applied to groups
only
Structured Query Language Basics
Implementing
Having Clause-I
Structured Query Language Basics
Write a query to display average salary of
each department if there are at least 2
employees working in the department and
minimum salary is more than average salary
by 100
Solution:
Implementing
Having Clause-II
Structured Query Language Basics
Write a query to display maximum and
minimum salary by each department if
average salary is more than 1500 of the
department and less than 3000. The employee
should not be included if there is any
occurrence of ‘A’ in the ename or earning no
commission and is hired at least six month
before
Structured Query Language Basics
Select max(sal) , min(sal) from emp
Where ename not like ‘%A%’ or comm is null
and months_between(sysdate, hiredate)>6
Group by deptno
Having max(sal) > 4500 and avg(sal)<1500;
Structured Query Language Basics
Order by Clause
Can use
independent of
where or group by
or having clause
Structured Query Language Basics
order by sal;
Structured Query Language Basics
What are Joins
Implementing
Join-I
Structured Query Language Basics
Implementing
Join-II
Structured Query Language Basics
Write a query to display list of employee name
and name of department of all those
employees who are hired at least 10 years
before and are working as Analyst
When PK and FK
belong to same
table
Implementing Self
Join-I
Structured Query Language Basics
Implementing Self
Join-II
Structured Query Language Basics
Solution
A subquery is used
to return data that
will be used in the
main query as a
condition to
further restrict the
data to be
retrieved.
Structured Query Language Basics
Implementing
SubQuery - I
Structured Query Language Basics
Write a query to display information of all
those employees who are earning minimum
salary
but employees are neither working as
Manager nor Clerk earning commission
Implementing
SubQuery - II
Structured Query Language Basics
Solution: