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

SQL Day 7 Subquries and Date Functions

Uploaded by

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

SQL Day 7 Subquries and Date Functions

Uploaded by

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

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.


Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.

SUBQUERIES

Nesting of queries, one within the other is termed as a subquery.


A statement containing a subquery is called a parent query.
Subqueries are used to retrieve data from tables that depend on the values in the
table
itself.

TYPES
* Single row subqueries
* Multi row subqueries
* Multiple subqueries
* Correlated subqueries

SINGLE ROW SUBQUERIES


In single row subquery, it will return one value

MULTI ROW SUBQUERIES


In multi row subquery, it will return more than one value. In such cases we should
include
operators like any, all, in or not in between the comparision operator and the
subquery.

MULTIPLE SUBQUERIES
There is no limit on the number of subqueries included in a where clause. It allows
nesting
of a query within a subquery.

CORRELATED SUBQUERIES
A subquery is evaluated once for the entire parent statement where as a correlated
subquery is evaluated once for every row processed by the parent statement

Single Row Sub Query

select * from employees where salary=(select max(salary) from employees)

select * from employees where salary=24000

===================================================================================
========================================================

Multiple column sub queries

select * from employees where (department_id,salary) in (select


department_id,max(salary) from employees group by department_id)

===================================================================================
========================================================

Inline View
From inside select query called inline view.

select max(sal) from (select department_id,max(salary)sal from employees group by


department_id)

===================================================================================
========================================================

Scalar Sub Query


Select statement inside the select statement that is called scalar sub query.

select first_name,salary,(select avg(salary) from employees) from employees

===================================================================================
========================================================
corelated subquery

Outer query refer inner query value or inner query refer the outer valuse that is
called co related sub query

select department_name from departments d where department_id in (select


department_id from employees where department_id=d.department_id)

===================================================================================
========================================================
where class inside select means nested sub query

select * from employees where salary in (select max(salary) from employees)

===================================================================================
========================================================

Date Functions

1. SYSDATE - Returns the system date


Select sysdate from dual
Select sysdate from Employees

2. SYSTIMESTAMP - Returns the date and time of the system date


Select Systimestamp from dual
Select Systimestamp from Employees

3.CURRENT DATE - Current Date


Select Current_date from Dual
Select Current_date from Employees

4. CURRENT TIME STAMP - Current Date with Time stamp


Select Current_timestamp from Dual
Select Current_timestamp from Employees

5. LOCAL TIME STAMP - Our timezone date time and date in the session time zone
Select Localtimestamp from Dual
Select Localtimestamp from Employees
6. DATA BASE TIME ZONE - Wrirtten Difference Between database Time Zone & Local
Time Zone
Select dbtimezone from Dual
Select dbtimezone from Employees

7. ADD MONTHS - Adding Number of months to the given date


Select Add_Months( DATE '2024-10-22', 1 ) from dual
Select Add_Months( sysdate , 1 ) from dual
Select Add_Months( Hire_date, 1 ) from Employees

8. Months Between - Written Number of months between two months


Select Months_Between( DATE '2024-10-22', DATE '2017-01-01' ) from dual
Select Months_Between( DATE '2017-07-01', sysdate ) from dual
Select Months_Between( Hire_Date, sysdate ) from Employees

9. Next Day - Written the next first day(sunday)


Select Next_Day( DATE '2024-10-22', 'SUNDAY' ) from dual
Select Next_Day( sysdate , 'SUNDAY' ) from dual
Select Next_Day( Hire_date , 'SUNDAY' ) from Employees

10. LAST DAY - Written last day of month


Select Last_Day(DATE '2024-10-22') from dual
Select Last_Day(SYSDATE) from dual
Select Last_Day(Hire_date) from Employees

11.TRUNC - Written Start date of Given date


Select TRUNC(DATE '2024-10-22', 'MM') from dual
Select TRUNC(SYSDATE,'MM') from dual
Select TRUNC(Hire_date,'MM') from Employees

12. EXTRACT - seperates individual part of date


Select Extract (Year from sysdate) from dual
Select Extract (Month from sysdate) from dual
Select Extract (Day from sysdate) from dual
Select Extract (Year from DATE '2024-10-22') from dual
Select Extract (Year from Hire_Date) from Employees

13. To_Date - Convert the Given Character to date value


Select To_Date( '22 Oct 2024', 'DD MON YYYY' ) from dual

Task:
1,Find out the Employees working dep and the dep names using subquery
2,what if employees layoff next week monday how to find the tenure months
3,Find the employee max salary with details
4,count the employee details who are woking accounts dep using sub query only
5,Find the new year date ?
6,Which weekday next new year will be starts ?

You might also like