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

SQL Day 7 Subquries and Date Functions - Queries

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

SQL Day 7 Subquries and Date Functions - Queries

Uploaded by

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

--SUB Queries - combination of two or More Queries to Provide a different Result

--1. Single Row Sub Query - one row Query with Simple Sub Query
Select * from Employees
Select Max(Salary)from Employees
Select * from Employees Where Salary = (Select Max(Salary)from Employees)
--2. NeSted Sub Query - will Have a Where Condition inside the Select Satement and
again a select Statement inside the where Condition It can be Used "n" Number of
times
Select * from Employees Where Salary = (Select Max(Salary)from Employees)
--3. Multiple column sub queries - Which can Acesses two or More Colum at a single
time
select * from employees where (department_id,salary) in (select
department_id,max(salary) from employees group by department_id)
--4.Inline View - The Query contain From Statement inside select Select Statement
is called inline view.
select max(sal) from (select department_id,max(salary)sal from employees group by
department_id)
--5. Scalar Sub Query - The Query contain Select statement inside the select
statement that is called scalar sub query.
select first_name,salary,(select Max(salary) MAX_Salary from employees) from
employees
--6. corelated subquery - Outer query refer inner query value or inner query refer
the outer valuse that is called co related sub query
Select * from Employees
Select * from Departments
select department_name from departments d where department_id in (select
department_id from employees where department_id=d.department_id)

--Date Functions - To work in order to the date Fiels which we having.


--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-10', 1 ) from dual
Select Add_Months( DATE '2024-10-22', -1 ) from dual
Select Add_Months( sysdate , 1 ) from dual
Select First_name, Hire_date, 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( sysdate, DATE '2018-01-01' ) from dual
Select Months_Between(sysdate, Hire_Date) 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 First_name, Hire_date,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 First_name, Hire_date,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 First_name, Hire_date, 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 '2020-10-22') from dual
Select First_name, Hire_date, 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

You might also like