Subquery. Subquery or Inner query or Nested query A subquery is a query within another query. The outer query is called as main query and The inner query.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Advertisements

Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
PL/SQL.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Introduction to Structured Query Language (SQL)
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
3-1 Copyright  Oracle Corporation, All rights reserved. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Single Row Functions Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number, date, general and conversion.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
SQL Oracle PL/SQL. Select SELECT column1, column2,...columnN FROM table_name WHERE condition; SELECT column1, column2,...columnN FROM table_name WHERE.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
INSERT Statement. 2 home back first prev next last What Will I Learn? Give examples of why it is important to be able to alter the data in a database.
Chapter 12 Subqueries and Merge Statements
INTRODUCTION TO SQL Chapter SELECT * FROM teacher WHERE INSTR (subject_id, ‘&1’)= 4 AND LOWER (subject_id) LIKE ‘HST%’ ; When prompted for the.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Lecture3b - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data Guide to Oracle 10g ITBIS373 Database Development.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
Writing Basic SQL SELECT Statements Lecture
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Single Row Functions. 3-2 Objectives Explain the various types of functions available in SQL. Explain the various types of functions available in SQL.
Single Row Functions Part I Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number and date SQL functions.
Chapter 5 Introduction to SQL.
Relational Database Design
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Insert, Update and the rest…
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Oracle9i: SQL
Using the Set Operators
Using the Set Operators
Computations Done on table data
Using Single-Row Functions to Customize Output
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Writing Basic SQL SELECT Statements
Contents Preface I Introduction Lesson Objectives I-2
Review SQL Advanced.
Database Systems: Design, Implementation, and Management Tenth Edition
Using the Set Operators
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Subquery

Subquery or Inner query or Nested query A subquery is a query within another query. The outer query is called as main query and The inner query is called as subquery. A subquery is usually added in the WHERE Clause of the sql statement.

Subquery Subquery or Inner query or Nested query Syntax The subquery (inner query) executes once before the main query (outer query) executes.

Subquery Subquery or Inner query or Nested query Two tables ‘STUDENT' and ‘MARKS' with common field ‘ID'. To write a query to identify all students who get better marks than that of the student who's ID is ‘2‘. If we know the mark of ID ’2’ then SELECT A.ID,A.NAME, B.MARK FROM STUDENT A, MARK B WHERE A.ID=B.ID AND B.MARK >70;

Subquery Subquery or Inner query or Nested query STUDENT MARK S But we do not know the marks of ‘2'. we require two queries (Nested query) One query returns the marks of '2' and Second query identifies the students who get better marks than the result of the first query SELECT A.ID,A.NAME, B.MARK FROM STUDENT A, MARKS B WHERE A.ID=B.ID AND B.MARK > (SELECT MARK FROM MARKS WHERE ID=2);

Subquery Rules for subqueries: Subqueries must be enclosed within parentheses. Inner query can have only one column in the SELECT clause, unless multiple columns are in the main query. Inner Query cannot use An ORDER BY clause, although the main query can use an ORDER BY. The BETWEEN operator cannot be used with a inner query; however, the BETWEEN operator can be used within the main query.

Subquery Subqueries can be used with SELECT INSERT UPDAT E, and DELET E statements along with the operators like =,, >=, <=, IN, BETWEEN etc.

Subquery Subquerie in select statement Tab1 Tab2 List all details from tab2, where rollno =4 in tab1 select * from tab2 where rollno in (select rollno from tab1 where rollno=4)

Subquery Subquerie in insert statement Tab1 Tab4 Consider table tab1 & tab4 with similar structure. To copy the records from tab1 to tab4 where rollno=3 insert into tab4 select * from tab1 where rollno in (select rollno from tab1 where rollno=3)

Subquery Subquerie in update statement Tab1 Tab2 Increment the marks with 10 in tab2, whose rollno = 4 in tab1 update tab2 set mark=mark+10 where rollno in (select rollno from tab1 where rollno=4)

Subquery Subquerie in delete statement Tab1 Tab2 Delete all details from tab2, where rollno is 4 in tab1 delete from tab2 where rollno in (select rollno from tab1 where rollno=4)

Subquery NULL Functions Consider the table N_TEST 3 rd and 2 nd row contains NULL values SELECT * FROM N_TEST WHERE COL1=NULL; Then result will no data found

Subquery NULL Functions Consider the table N_TEST 3 rd and 2 nd row contains NULL values Instead we must use the IS NULL or IS NOT NULL operators SELECT * FROM N_TEST WHERE COL1 IS NULL; Then SELECT * FROM N_TEST WHERE COL1 IS NOT NULL;

Subquery NULL Functions NVL The NVL function allows to replace null values with a default value. If the value in the first parameter is null, the function returns the value in the second parameter. If the first parameter is any value other than null, it is returned unchanged.

Subquery NULL Functions NVL COL1 in the N_TEST table contains null in all rows except the first & second Using the NVL function we replace the null values with ‘AJI'. SELECT COL1,NVL(COL2,'AJI') AS OUTPUT FROM N_TEST;

Subquery NULL Functions DECODE The DECODE function have 3 parameters. If second parameter occurs will replace to third parameter Other wise it return NULL SELECT COL1,DECODE(COL2,NULL,'AJI') AS OUTPUT FROM N_TEST; Here all NULL, converted to ‘AJI’ other wise it converted to NULL

Subquery NULL Functions NVL2 The NVL2 function accepts three parameters. If the first parameter value is not null it returns the value in the second parameter. If the first parameter value is null, it returns the third parameter. SELECT COL1,NVL2(COL2,'SAJI','AJI') AS OUTPUT FROM N_TEST;

Subquery Numeric Functions ABS(number) Returns the absolute positive value of an expression. SELECT ABS(COL1) AS OUTPUT FROM N_TEST;

Subquery Numeric Functions MOD(number, divisor) Returns the remainder of the division from 2 integer values. SELECT COL1,MOD(COL1,2) AS OUTPUT FROM N_TEST

Subquery Numeric Functions POWER(number, power) Returns the exponential value for the numeric expression. SELECT COL1,POWER(COL1,2) AS OUTPUT FROM N_TEST

Subquery String Functions CONCAT(string1, string2) Join 2 strings SELECT COL1,CONCAT(COL1,'AJI') AS OUTPUT FROM N_TEST

Subquery String Functions INITCAP(string) Returns a string with each word's first character in uppercase and the rest in lowercase. SELECT COL1,INITCAP(COL2) AS OUTPUT FROM N_TEST

Subquery String Functions REPLACE(string,searchString,replacement) Used to replace the string1, having string2 with string3. SELECT COL1,REPLACE(COL2,'one','TWO') AS OUTPUT FROM N_TEST

Subquery String Functions SUBSTR(string,start,length) Displays the string Starting with the character in string and select the next characters. SELECT COL1,SUBSTR(COL2,2,2) AS SUBSTR FROM N_TEST

Subquery Date Functions SYSDATE Returns the current date Tab1 SELECT SYSDATE FROM TAB1;

Subquery Date Functions DD Addition of date SELECT DOB,DOB+2,DOB+30 FROM DD;

Subquery Date Functions MONTHS_BETWEEN returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. Syntax MONTHS_BETWEEN(date1,date2); Other wise negative SELECT DOB, MONTHS_BETWEEN('10-FEB-1998',DOB) FROM DD

Subquery Date Functions ADD_MONTHS Used to add month Syntax ADD_MONTHS(date,int); Add DOB month with 2 SELECT DOB, ADD_MONTHS(DOB,2) FROM DD;

Subquery Date Functions LAST_DAY(date1) Find the date of the last day of the month that contains date1. SELECT DOB, LAST_DAY(DOB) FROM DD;

Subquery Date Functions TRUNC(date1,’char’) Find the date of first day of the month containing in date1 when char=‘MONTH’. If char=‘YEAR’, it finds first day of year containing date1. SELECT DOB, TRUNC(DOB,'MONTH'),TRUNC(DOB,'YEAR') FROM DD;

Subquery Date Functions TO_CHAR() SELECT DOB, TO_CHAR(DOB,'DAY-DDTH- MONTH-YEAR') FROM DD;

Subquery Date Functions TO_DATE converts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a value of DATE datatype. SELECT DOB, TO_DATE('FEB ','MONTH DD YYYY') FROM DD;

Subquery Date Functions

Subquery Date Functions