SlideShare a Scribd company logo
1 of 23
SQL Query Slides
1By:-Gourav Kottawar
Retrieval Queries in SQL
Basic form of the SQL SELECT statement is called a mapping or a
SELECT-FROM-WHERE block
SELECT <attribute list>
FROM <table list>
WHERE <condition>
– <attribute list> is a list of attribute names whose values are to
be
retrieved by the query
– <table list> is a list of the relation names required to process
the
query
– <condition> is a conditional (Boolean) expression that identifies
the tuples to be retrieved by the query
2By:-Gourav Kottawar
Relational Database schema
3By:-Gourav Kottawar
Populated
Database:
4By:-Gourav Kottawar
Simple Queries
Query 0: Retrieve the birthdate and address of the employee whose
name is 'John B. Smith'.
Query 1: Retrieve the name and address of all employees who work for
the 'Research' department.
Q0: SELECT BDATE, ADDRESS
FROM EMPLOYEE
WHERE FNAME='John' AND MINIT='B’
AND LNAME='Smith’
Q1: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND
DNUMBER=DNO
5By:-Gourav Kottawar
Some Queries Cont.
Q2: SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND
PLOCATION='Stafford'
Query 3: For each employee, retrieve the employee's name, and the name
of his or her immediate supervisor.
Q3: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE E S
WHERE E.SUPERSSN=S.SSN
Query 2: For every project located in 'Stafford', list the project number, the
controlling department number, and the department manager's last name,
address, and birthdate.
6By:-Gourav Kottawar
Some Queries Cont.
Q4: (SELECT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND
LNAME='Smith')
UNION (SELECT PNAME
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN AND
LNAME='Smith')
Query 4: Make a list of all project numbers for projects that involve an
employee whose last name is 'Smith' as a worker or as a manager of
the department that controls the project.
7By:-Gourav Kottawar
Some Queries Cont.
Q5: SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN (SELECT ESSN
FROM DEPENDENT
WHERE ESSN=E.SSN AND
E.FNAME=DEPENDENT_NAME)
Query 5: Retrieve the name of each employee who has a dependent
with the same first name as the employee.
Q5A: SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E, DEPENDENT D
WHERE E.SSN=D.ESSN AND
E.FNAME=D.DEPENDENT_NAME
The comparison operator IN compares a value v with a set (or multi-set)
of values V, and evaluates to TRUE if v is one of the elements in V
8By:-Gourav Kottawar
Some Queries Cont. EXISTS
Q5B: SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE EXISTS (SELECT *
FROM DEPENDENT
WHERE SSN=ESSN AND
FNAME=DEPENDENT_NAME)
EXISTS is used to check whether the result of a correlated nested query
is empty (contains no tuples) or not
9By:-Gourav Kottawar
Some Queries Cont.
explicit (enumerated) set of values
Query 6: Retrieve the social security numbers of all employees who work
on project number 1, 2, or 3.
Q6: SELECT DISTINCT ESSN
FROM WORKS_ON
WHERE PNO IN (1, 2, 3)
It is also possible to use an explicit (enumerated) set of values in the
WHERE-clause rather than a nested query
10By:-Gourav Kottawar
Some Queries Cont.
Query 7: Retrieve the name of each employee who works on all the projects
controlled by department number 5.
Q7: SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE ( (SELECT PNO
FROM WORKS_ON
WHERE SSN=ESSN)
CONTAINS
(SELECT PNUMBER
FROM PROJECT
WHERE DNUM=5) )
The CONTAINS operator compares two sets of values , and returns TRUE
if one set contains all values in the other set (reminiscent of the division
operation of algebra).
11By:-Gourav Kottawar
Some Queries Cont. Null Value
Query 8: Retrieve the names of all employees who do not have supervisors.
Q8: SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE SUPERSSN IS NULL
SQL uses IS or IS NOT to compare NULLs because it considers each NULL
value distinct from other NULL
Note: If a join condition is specified, tuples with NULL values for the join
attributes are not included in the result
12By:-Gourav Kottawar
Some Queries Cont. JOIN
Can be written as:
QTA: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEES
ON E.SUPERSSN=S.SSN)
QT: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE E S
WHERE E.SUPERSSN=S.SSN
13By:-Gourav Kottawar
Some Queries Cont. JOIN
Can be written as:
Q9A: SELECT FNAME, LNAME, ADDRESS
FROM (EMPLOYEE JOIN DEPARTMENT
ON DNUMBER=DNO)
WHERE DNAME='Research’
Q9: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND DNUMBER=DNO
Or as:
Q9B: SELECT FNAME, LNAME, ADDRESS
FROM (EMPLOYEE NATURAL JOIN
DEPARTMENT AS DEPT(DNAME, DNO, MSSN, MSDATE)
WHERE DNAME='Research’
14By:-Gourav Kottawar
Joined Relations Feature
in SQL2
Query 2: For every project located in 'Stafford', list the project number, the
controlling department number, and the department manager's last name,
address, and birthdate.
Q2 B: SELECT PNUMBER, DNUM,
LNAME, BDATE, ADDRESS
FROM (PROJECT JOIN
DEPARTMENT ON
DNUM=DNUMBER) JOIN
EMPLOYEE ON
MGRSSN=SSN) )
WHERE PLOCATION='Stafford’
15By:-Gourav Kottawar
AGGREGATE FUNCTIONS
Query 10: Find the maximum salary, the minimum salary, and
the average salary among all employees.
Q10: SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY)
FROM EMPLOYEE
Include COUNT, SUM, MAX, MIN, and AVG
Query 11: Find the maximum salary, the minimum salary, and the average
salary among employees who work for the 'Research' department.
Q11: SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY)
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER AND
DNAME='Research'
16By:-Gourav Kottawar
Group by
Query 12: For each department, retrieve the department number, the
number of employees in the department, and their average salary.
Q12: SELECT DNO, COUNT (*), AVG (SALARY)
FROM EMPLOYEE
GROUP BY DNO
SQL has a GROUP BY-clause for specifying the grouping attributes, which
must also appear in the SELECT-clause
Query 13: For each project, retrieve the project number, project
name, and the number of employees who work on that project.
Q13: SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER, PNAME
17By:-Gourav Kottawar
Group by cont. Having
Query 14: For each project on which more than two employees work,
retrieve the project number, project name, and the number of employees
who work on that project.
Q14: SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER, PNAME
HAVING COUNT (*) > 2
lThe HAVING-clause is used for specifying a selection condition on groups
(rather than on individual tuples)
18By:-Gourav Kottawar
Summary of SQL Queries
 A query in SQL can consist of up to six clauses, but
only
the first two, SELECT and FROM, are mandatory. The
clauses are specified in the following order:
SELECT <attribute list>
FROM <table list>
[WHERE <condition>]
[GROUP BY <grouping attribute(s)>]
[HAVING <group condition>]
[ORDER BY <attribute list>]
19By:-Gourav Kottawar
Summary of SQL Queries
(cont.)
 The SELECT-clause lists the attributes or functions to
be retrieved
 The FROM-clause specifies all relations (or aliases)
needed in the query but not those needed in nested
queries
 The WHERE-clause specifies the conditions for
selection and join of tuples from the relations
specified in the FROM-clause
 GROUP BY specifies grouping attributes
 HAVING specifies a condition for selection of groups
 ORDER BY specifies an order for displaying the result
of a query
 A query is evaluated by first applying the WHERE-
clause, then
 GROUP BY and HAVING, and finally the SELECT-
clause
20By:-Gourav Kottawar
More complex Select “SQL Server”
SELECT [ ALL | DISTINCT ]
[ TOP n [ PERCENT ] [ WITH TIES ] ]
< select_list >
< select_list > ::=
{ *
| { table_name | view_name | table_alias }.*
| { column_name | expression | IDENTITYCOL |
ROWGUIDCOL }
[ [ AS ] column_alias ]
| column_alias = expression
} [ ,...n ]
SELECT select_list
[ INTO new_table ]
FROM table_source
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC |
DESC ] ]
Select Clause:
From Clause:
[ FROM { < table_source > } [ ,...n ] ]
< table_source > ::=
table_name [ [ AS ] table_alias ] [ WITH ( < table_hint > [
,...n ] ) ]
| view_name [ [ AS ] table_alias ]
| rowset_function [ [ AS ] table_alias ]
| OPENXML
| derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ]
| < joined_table >
< joined_table > ::=
< table_source > < join_type > < table_source > ON <
search_condition >
| < table_source > CROSS JOIN < table_source >
| < joined_table >
< join_type > ::=
[ INNER | { { LEFT | RIGHT | FULL } [ OUTER ] } ]
[ < join_hint > ]
JOIN
Arguments
< table_source >
21By:-Gourav Kottawar
More complex Select “SQL Server”
Cont.
Where Clause:
[ WHERE < search_condition > | <
old_outer_join > ]
< old_outer_join > ::=
column_name { * = | = * } column_name
Group by clause:
[ GROUP BY [ ALL ] group_by_expression
[ ,...n ]
[ WITH { CUBE | ROLLUP } ]
]
Having:
[ HAVING < search_condition > ]
Order By Clause:
[ ORDER BY { order_by_expression [ ASC |
DESC ] } [ ,...n] ]
Compute Clause:
[ COMPUTE
{ { AVG | COUNT | MAX | MIN | STDEV | STDEVP
| VAR | VARP | SUM }
( expression ) } [ ,...n ]
[ BY expression [ ,...n ] ]
]
22By:-Gourav Kottawar
Compute
Row aggregate
function Result
AVG Average of the values in the numeric expression
COUNT Number of selected rows
MAX Highest value in the expression
MIN Lowest value in the expression
STDEV Statistical standard deviation for all values in the expression
STDEVP |Statistical standard deviation for the population for all values in the expression
SUM Total of the values in the numeric expression
VAR Statistical variance for all values in the expression
VARP Statistical variance for the population for all values in the expression
23By:-Gourav Kottawar

More Related Content

What's hot

Inventory management system
Inventory management systemInventory management system
Inventory management systemcopo7475
 
CURRICULUM_VITAE_Store Keeper
CURRICULUM_VITAE_Store KeeperCURRICULUM_VITAE_Store Keeper
CURRICULUM_VITAE_Store Keepermuneer hawa
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answersNawaz Sk
 
SQL practice questions - set 3
SQL practice questions - set 3SQL practice questions - set 3
SQL practice questions - set 3Mohd Tousif
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2Mohd Tousif
 
Sql task answers
Sql task answersSql task answers
Sql task answersNawaz Sk
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answersMichael Belete
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sqlN.Jagadish Kumar
 
Bsc cs ii-dbms-u-iv-normalization
Bsc cs ii-dbms-u-iv-normalizationBsc cs ii-dbms-u-iv-normalization
Bsc cs ii-dbms-u-iv-normalizationRai University
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands1keydata
 
Hospital management system in java
Hospital management system in javaHospital management system in java
Hospital management system in javaVarun Yadav
 
Types & Fundamentals of Information System
Types & Fundamentals of Information SystemTypes & Fundamentals of Information System
Types & Fundamentals of Information SystemAwais Mansoor Chohan
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)Sabana Maharjan
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Achmad Solichin
 

What's hot (20)

All questions
All questionsAll questions
All questions
 
Inventory management system
Inventory management systemInventory management system
Inventory management system
 
SQL BASIC QUERIES
SQL  BASIC QUERIES SQL  BASIC QUERIES
SQL BASIC QUERIES
 
CURRICULUM_VITAE_Store Keeper
CURRICULUM_VITAE_Store KeeperCURRICULUM_VITAE_Store Keeper
CURRICULUM_VITAE_Store Keeper
 
Plsql task answers
Plsql task answersPlsql task answers
Plsql task answers
 
SQL practice questions - set 3
SQL practice questions - set 3SQL practice questions - set 3
SQL practice questions - set 3
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
 
Sql task answers
Sql task answersSql task answers
Sql task answers
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answers
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Dbms normalization
Dbms normalizationDbms normalization
Dbms normalization
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Bsc cs ii-dbms-u-iv-normalization
Bsc cs ii-dbms-u-iv-normalizationBsc cs ii-dbms-u-iv-normalization
Bsc cs ii-dbms-u-iv-normalization
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Hospital management system in java
Hospital management system in javaHospital management system in java
Hospital management system in java
 
Types & Fundamentals of Information System
Types & Fundamentals of Information SystemTypes & Fundamentals of Information System
Types & Fundamentals of Information System
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 

Viewers also liked

sql statement
sql statementsql statement
sql statementzx25 zx25
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questionsPyadav010186
 
Consultas de tablas con comando de SQL
Consultas de tablas  con comando de SQLConsultas de tablas  con comando de SQL
Consultas de tablas con comando de SQLYarquiri Claudio
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101IDERA Software
 
SQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersSQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersIván Stepaniuk
 
Bases de Datos Cap-V SQL: Manipulación de datos
Bases de Datos Cap-V SQL: Manipulación de datosBases de Datos Cap-V SQL: Manipulación de datos
Bases de Datos Cap-V SQL: Manipulación de datosVideoconferencias UTPL
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group FunctionsSalman Memon
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sqlCharan Reddy
 
Complete Sql Server querries
Complete Sql Server querriesComplete Sql Server querries
Complete Sql Server querriesIbrahim Jutt
 
Travel research-2015-boomer-travel-trends-infographic-aarp-res-gen
Travel research-2015-boomer-travel-trends-infographic-aarp-res-genTravel research-2015-boomer-travel-trends-infographic-aarp-res-gen
Travel research-2015-boomer-travel-trends-infographic-aarp-res-genNext Avenue
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sqlVivek Singh
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functionssinhacp
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Finalmukesh24pandey
 
PostgreSQL - Lección 4 - Usando funciones para manipular grupos de datos
PostgreSQL - Lección 4 - Usando funciones para manipular grupos de datosPostgreSQL - Lección 4 - Usando funciones para manipular grupos de datos
PostgreSQL - Lección 4 - Usando funciones para manipular grupos de datosNicola Strappazzon C.
 

Viewers also liked (20)

sql statement
sql statementsql statement
sql statement
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
Consultas de tablas con comando de SQL
Consultas de tablas  con comando de SQLConsultas de tablas  con comando de SQL
Consultas de tablas con comando de SQL
 
consultas en sql server
consultas en sql serverconsultas en sql server
consultas en sql server
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101
 
Ejercicios sql
Ejercicios sqlEjercicios sql
Ejercicios sql
 
SQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersSQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholders
 
Bases de Datos Cap-V SQL: Manipulación de datos
Bases de Datos Cap-V SQL: Manipulación de datosBases de Datos Cap-V SQL: Manipulación de datos
Bases de Datos Cap-V SQL: Manipulación de datos
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 
Complete Sql Server querries
Complete Sql Server querriesComplete Sql Server querries
Complete Sql Server querries
 
Travel research-2015-boomer-travel-trends-infographic-aarp-res-gen
Travel research-2015-boomer-travel-trends-infographic-aarp-res-genTravel research-2015-boomer-travel-trends-infographic-aarp-res-gen
Travel research-2015-boomer-travel-trends-infographic-aarp-res-gen
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sql
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Sql wksht-5
Sql wksht-5Sql wksht-5
Sql wksht-5
 
SQL Join Basic
SQL Join BasicSQL Join Basic
SQL Join Basic
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
PostgreSQL - Lección 4 - Usando funciones para manipular grupos de datos
PostgreSQL - Lección 4 - Usando funciones para manipular grupos de datosPostgreSQL - Lección 4 - Usando funciones para manipular grupos de datos
PostgreSQL - Lección 4 - Usando funciones para manipular grupos de datos
 
SQL | Computer Science
SQL | Computer ScienceSQL | Computer Science
SQL | Computer Science
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 

Similar to SQL querys in detail || Sql query slides

Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxHAMEEDHUSSAINBU21CSE
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Vidyasagar Mundroy
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptxANSHVAJPAI
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricksYanli Liu
 
Adbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queriesAdbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queriesVaibhav Khanna
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimizationVivek Singh
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankpptnewrforce
 
Sql Patterns
Sql PatternsSql Patterns
Sql Patternsphanleson
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库renguzi
 
MySQL 8.0 NF : Common Table Expressions (CTE)
MySQL 8.0 NF : Common Table Expressions (CTE)MySQL 8.0 NF : Common Table Expressions (CTE)
MySQL 8.0 NF : Common Table Expressions (CTE)I Goo Lee
 

Similar to SQL querys in detail || Sql query slides (20)

Chapter08
Chapter08Chapter08
Chapter08
 
UNIT-3.pptx
UNIT-3.pptxUNIT-3.pptx
UNIT-3.pptx
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
 
Module03
Module03Module03
Module03
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Sql (DBMS)
Sql (DBMS)Sql (DBMS)
Sql (DBMS)
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Adbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queriesAdbms 21 sql 99 schema definition constraints and queries
Adbms 21 sql 99 schema definition constraints and queries
 
01 basic orders
01   basic orders01   basic orders
01 basic orders
 
Sql select
Sql select Sql select
Sql select
 
Updat Dir
Updat DirUpdat Dir
Updat Dir
 
Sql server lab_3
Sql server lab_3Sql server lab_3
Sql server lab_3
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Sql Patterns
Sql PatternsSql Patterns
Sql Patterns
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
 
MySQL 8.0 NF : Common Table Expressions (CTE)
MySQL 8.0 NF : Common Table Expressions (CTE)MySQL 8.0 NF : Common Table Expressions (CTE)
MySQL 8.0 NF : Common Table Expressions (CTE)
 
Sql
SqlSql
Sql
 

More from gourav kottawar

operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cppgourav kottawar
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
classes & objects in cpp
classes & objects in cppclasses & objects in cpp
classes & objects in cppgourav kottawar
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overviewgourav kottawar
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cppgourav kottawar
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cppgourav kottawar
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basicsgourav kottawar
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overviewgourav kottawar
 
SQL || overview and detailed information about Sql
SQL || overview and detailed information about SqlSQL || overview and detailed information about Sql
SQL || overview and detailed information about Sqlgourav kottawar
 
Rrelational algebra in dbms overview
Rrelational algebra in dbms overviewRrelational algebra in dbms overview
Rrelational algebra in dbms overviewgourav kottawar
 
overview of database concept
overview of database conceptoverview of database concept
overview of database conceptgourav kottawar
 
Relational Model in dbms & sql database
Relational Model in dbms & sql databaseRelational Model in dbms & sql database
Relational Model in dbms & sql databasegourav kottawar
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptgourav kottawar
 
security and privacy in dbms and in sql database
security and privacy in dbms and in sql databasesecurity and privacy in dbms and in sql database
security and privacy in dbms and in sql databasegourav kottawar
 

More from gourav kottawar (20)

operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
classes & objects in cpp
classes & objects in cppclasses & objects in cpp
classes & objects in cpp
 
expression in cpp
expression in cppexpression in cpp
expression in cpp
 
basics of c++
basics of c++basics of c++
basics of c++
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basics
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
basics of c++
basics of c++basics of c++
basics of c++
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
 
expression in cpp
expression in cppexpression in cpp
expression in cpp
 
SQL || overview and detailed information about Sql
SQL || overview and detailed information about SqlSQL || overview and detailed information about Sql
SQL || overview and detailed information about Sql
 
Rrelational algebra in dbms overview
Rrelational algebra in dbms overviewRrelational algebra in dbms overview
Rrelational algebra in dbms overview
 
overview of database concept
overview of database conceptoverview of database concept
overview of database concept
 
Relational Model in dbms & sql database
Relational Model in dbms & sql databaseRelational Model in dbms & sql database
Relational Model in dbms & sql database
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
 
security and privacy in dbms and in sql database
security and privacy in dbms and in sql databasesecurity and privacy in dbms and in sql database
security and privacy in dbms and in sql database
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 

Recently uploaded (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

SQL querys in detail || Sql query slides

  • 2. Retrieval Queries in SQL Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE block SELECT <attribute list> FROM <table list> WHERE <condition> – <attribute list> is a list of attribute names whose values are to be retrieved by the query – <table list> is a list of the relation names required to process the query – <condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query 2By:-Gourav Kottawar
  • 5. Simple Queries Query 0: Retrieve the birthdate and address of the employee whose name is 'John B. Smith'. Query 1: Retrieve the name and address of all employees who work for the 'Research' department. Q0: SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME='John' AND MINIT='B’ AND LNAME='Smith’ Q1: SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO 5By:-Gourav Kottawar
  • 6. Some Queries Cont. Q2: SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford' Query 3: For each employee, retrieve the employee's name, and the name of his or her immediate supervisor. Q3: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E S WHERE E.SUPERSSN=S.SSN Query 2: For every project located in 'Stafford', list the project number, the controlling department number, and the department manager's last name, address, and birthdate. 6By:-Gourav Kottawar
  • 7. Some Queries Cont. Q4: (SELECT PNAME FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND LNAME='Smith') UNION (SELECT PNAME FROM PROJECT, WORKS_ON, EMPLOYEE WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME='Smith') Query 4: Make a list of all project numbers for projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project. 7By:-Gourav Kottawar
  • 8. Some Queries Cont. Q5: SELECT E.FNAME, E.LNAME FROM EMPLOYEE AS E WHERE E.SSN IN (SELECT ESSN FROM DEPENDENT WHERE ESSN=E.SSN AND E.FNAME=DEPENDENT_NAME) Query 5: Retrieve the name of each employee who has a dependent with the same first name as the employee. Q5A: SELECT E.FNAME, E.LNAME FROM EMPLOYEE E, DEPENDENT D WHERE E.SSN=D.ESSN AND E.FNAME=D.DEPENDENT_NAME The comparison operator IN compares a value v with a set (or multi-set) of values V, and evaluates to TRUE if v is one of the elements in V 8By:-Gourav Kottawar
  • 9. Some Queries Cont. EXISTS Q5B: SELECT FNAME, LNAME FROM EMPLOYEE WHERE EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN AND FNAME=DEPENDENT_NAME) EXISTS is used to check whether the result of a correlated nested query is empty (contains no tuples) or not 9By:-Gourav Kottawar
  • 10. Some Queries Cont. explicit (enumerated) set of values Query 6: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. Q6: SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1, 2, 3) It is also possible to use an explicit (enumerated) set of values in the WHERE-clause rather than a nested query 10By:-Gourav Kottawar
  • 11. Some Queries Cont. Query 7: Retrieve the name of each employee who works on all the projects controlled by department number 5. Q7: SELECT FNAME, LNAME FROM EMPLOYEE WHERE ( (SELECT PNO FROM WORKS_ON WHERE SSN=ESSN) CONTAINS (SELECT PNUMBER FROM PROJECT WHERE DNUM=5) ) The CONTAINS operator compares two sets of values , and returns TRUE if one set contains all values in the other set (reminiscent of the division operation of algebra). 11By:-Gourav Kottawar
  • 12. Some Queries Cont. Null Value Query 8: Retrieve the names of all employees who do not have supervisors. Q8: SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL SQL uses IS or IS NOT to compare NULLs because it considers each NULL value distinct from other NULL Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the result 12By:-Gourav Kottawar
  • 13. Some Queries Cont. JOIN Can be written as: QTA: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEES ON E.SUPERSSN=S.SSN) QT: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E S WHERE E.SUPERSSN=S.SSN 13By:-Gourav Kottawar
  • 14. Some Queries Cont. JOIN Can be written as: Q9A: SELECT FNAME, LNAME, ADDRESS FROM (EMPLOYEE JOIN DEPARTMENT ON DNUMBER=DNO) WHERE DNAME='Research’ Q9: SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO Or as: Q9B: SELECT FNAME, LNAME, ADDRESS FROM (EMPLOYEE NATURAL JOIN DEPARTMENT AS DEPT(DNAME, DNO, MSSN, MSDATE) WHERE DNAME='Research’ 14By:-Gourav Kottawar
  • 15. Joined Relations Feature in SQL2 Query 2: For every project located in 'Stafford', list the project number, the controlling department number, and the department manager's last name, address, and birthdate. Q2 B: SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM (PROJECT JOIN DEPARTMENT ON DNUM=DNUMBER) JOIN EMPLOYEE ON MGRSSN=SSN) ) WHERE PLOCATION='Stafford’ 15By:-Gourav Kottawar
  • 16. AGGREGATE FUNCTIONS Query 10: Find the maximum salary, the minimum salary, and the average salary among all employees. Q10: SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROM EMPLOYEE Include COUNT, SUM, MAX, MIN, and AVG Query 11: Find the maximum salary, the minimum salary, and the average salary among employees who work for the 'Research' department. Q11: SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER AND DNAME='Research' 16By:-Gourav Kottawar
  • 17. Group by Query 12: For each department, retrieve the department number, the number of employees in the department, and their average salary. Q12: SELECT DNO, COUNT (*), AVG (SALARY) FROM EMPLOYEE GROUP BY DNO SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT-clause Query 13: For each project, retrieve the project number, project name, and the number of employees who work on that project. Q13: SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME 17By:-Gourav Kottawar
  • 18. Group by cont. Having Query 14: For each project on which more than two employees work, retrieve the project number, project name, and the number of employees who work on that project. Q14: SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME HAVING COUNT (*) > 2 lThe HAVING-clause is used for specifying a selection condition on groups (rather than on individual tuples) 18By:-Gourav Kottawar
  • 19. Summary of SQL Queries  A query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory. The clauses are specified in the following order: SELECT <attribute list> FROM <table list> [WHERE <condition>] [GROUP BY <grouping attribute(s)>] [HAVING <group condition>] [ORDER BY <attribute list>] 19By:-Gourav Kottawar
  • 20. Summary of SQL Queries (cont.)  The SELECT-clause lists the attributes or functions to be retrieved  The FROM-clause specifies all relations (or aliases) needed in the query but not those needed in nested queries  The WHERE-clause specifies the conditions for selection and join of tuples from the relations specified in the FROM-clause  GROUP BY specifies grouping attributes  HAVING specifies a condition for selection of groups  ORDER BY specifies an order for displaying the result of a query  A query is evaluated by first applying the WHERE- clause, then  GROUP BY and HAVING, and finally the SELECT- clause 20By:-Gourav Kottawar
  • 21. More complex Select “SQL Server” SELECT [ ALL | DISTINCT ] [ TOP n [ PERCENT ] [ WITH TIES ] ] < select_list > < select_list > ::= { * | { table_name | view_name | table_alias }.* | { column_name | expression | IDENTITYCOL | ROWGUIDCOL } [ [ AS ] column_alias ] | column_alias = expression } [ ,...n ] SELECT select_list [ INTO new_table ] FROM table_source [ WHERE search_condition ] [ GROUP BY group_by_expression ] [ HAVING search_condition ] [ ORDER BY order_expression [ ASC | DESC ] ] Select Clause: From Clause: [ FROM { < table_source > } [ ,...n ] ] < table_source > ::= table_name [ [ AS ] table_alias ] [ WITH ( < table_hint > [ ,...n ] ) ] | view_name [ [ AS ] table_alias ] | rowset_function [ [ AS ] table_alias ] | OPENXML | derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ] | < joined_table > < joined_table > ::= < table_source > < join_type > < table_source > ON < search_condition > | < table_source > CROSS JOIN < table_source > | < joined_table > < join_type > ::= [ INNER | { { LEFT | RIGHT | FULL } [ OUTER ] } ] [ < join_hint > ] JOIN Arguments < table_source > 21By:-Gourav Kottawar
  • 22. More complex Select “SQL Server” Cont. Where Clause: [ WHERE < search_condition > | < old_outer_join > ] < old_outer_join > ::= column_name { * = | = * } column_name Group by clause: [ GROUP BY [ ALL ] group_by_expression [ ,...n ] [ WITH { CUBE | ROLLUP } ] ] Having: [ HAVING < search_condition > ] Order By Clause: [ ORDER BY { order_by_expression [ ASC | DESC ] } [ ,...n] ] Compute Clause: [ COMPUTE { { AVG | COUNT | MAX | MIN | STDEV | STDEVP | VAR | VARP | SUM } ( expression ) } [ ,...n ] [ BY expression [ ,...n ] ] ] 22By:-Gourav Kottawar
  • 23. Compute Row aggregate function Result AVG Average of the values in the numeric expression COUNT Number of selected rows MAX Highest value in the expression MIN Lowest value in the expression STDEV Statistical standard deviation for all values in the expression STDEVP |Statistical standard deviation for the population for all values in the expression SUM Total of the values in the numeric expression VAR Statistical variance for all values in the expression VARP Statistical variance for the population for all values in the expression 23By:-Gourav Kottawar