PRESTIGE INSTITUTE OF MANAGEMENT
GWALIOR
Presented by- Arpit bhadoriya
BCA 2nd
(B)
Syntax for SELECT statement
• Clauses must be written in the following order
▫ SELECT
▫ FROM
▫ WHERE
▫ GROUP BY
▫ HAVING
▫ ORDER BY
SELECT clause
▫ The main element in a SQL query is the SELECT
statement.
▫ Figure out what values will actually be included in the
final result set by processing the SELECT clause.
▫ A properly written SELECT statement will always
produce a result in the form of one or more rows of
output.
▫ The SELECT statement chooses (selects) rows from
one or more tables according to specific criteria.
FROM clause
▫ FROM clause is used to specify the table name on
which we want to perform the operation.
▫ Without a table name, the database management
system does not know which table to query.
Example
Select *
From student;
• This query selects rows from the “student” table.
• The asterisk (*) tells SQL to select (display) all columns
contained in the table “student”.
WHERE clause
▫ Specific rows can be selected by adding a WHERE
clause to the SELECT query
If the result of the WHERE clause for that row is
TRUE then the row is kept. If the result of the
WHERE clause for that row is FALSE then the row
is "thrown away".
Example
select *
from student
where stu_id=2
Stu_id Stu_name Stu_add
2 Bhupendra Tansen nagar
GROUP BY
▫ The function to divide the tuples into groups and
returns an aggregate for each group .
▫ Usually, it is an aggregate function’s companion.
Example
419pizza
500hotdog
totalSoldfood
70pizza06/06/13
500hotdog06/06/13
349pizza06/05/13
soldfooddate
SELECT food, sum(sold) as totalSold
FROM Foodchart
group by food;
FoodChart
HAVING
▫ The substitute of WHERE for aggregate functions
▫ Usually, it is an aggregate function’s companion
SELECT food, sum(sold) as totalSold
FROM Foodchart
group by food
having sum(sold) > 450;
500hotdog
totalSoldfood
70pizza06/06/13
500hotdog06/06/13
349pizza06/05/13
soldfooddate
ORDER BY
▫ Sort the result set in the order specified in the
ORDER BY clause
▫ To sort columns from high to low or low to high ,
keyword ASC and DESC must be specified.
ASC - Ascending, low to high
DESC - Descending, high to low
When ASC or DESC is used, it must be followed by the column
name
Example
Select *
From student
Order by stu_add ASC
Stu_id Stu_name Stu_add
1 arpit adityapuram
3 abhishek D d nagar
2 bhupendra Tansen nagar
WHERE VS HAVING
• Similarities:
▫ The WHERE and HAVING clauses are both used to
exclude records from the result set.
• Differences
▫ WHERE clause
 The WHERE clause is processed before the groups are created
 Therefore, the WHERE clause can refer to any value in the
original tables
▫ HAVING clause
 The HAVING clause is processed after the groups are created
 The HAVING clause CANNOT refer to individual columns
from a table that are not also part of the group.
SQL select clause

SQL select clause

  • 1.
    PRESTIGE INSTITUTE OFMANAGEMENT GWALIOR Presented by- Arpit bhadoriya BCA 2nd (B)
  • 2.
    Syntax for SELECTstatement • Clauses must be written in the following order ▫ SELECT ▫ FROM ▫ WHERE ▫ GROUP BY ▫ HAVING ▫ ORDER BY
  • 3.
    SELECT clause ▫ Themain element in a SQL query is the SELECT statement. ▫ Figure out what values will actually be included in the final result set by processing the SELECT clause. ▫ A properly written SELECT statement will always produce a result in the form of one or more rows of output. ▫ The SELECT statement chooses (selects) rows from one or more tables according to specific criteria.
  • 4.
    FROM clause ▫ FROMclause is used to specify the table name on which we want to perform the operation. ▫ Without a table name, the database management system does not know which table to query.
  • 5.
    Example Select * From student; •This query selects rows from the “student” table. • The asterisk (*) tells SQL to select (display) all columns contained in the table “student”.
  • 6.
    WHERE clause ▫ Specificrows can be selected by adding a WHERE clause to the SELECT query If the result of the WHERE clause for that row is TRUE then the row is kept. If the result of the WHERE clause for that row is FALSE then the row is "thrown away".
  • 7.
    Example select * from student wherestu_id=2 Stu_id Stu_name Stu_add 2 Bhupendra Tansen nagar
  • 8.
    GROUP BY ▫ Thefunction to divide the tuples into groups and returns an aggregate for each group . ▫ Usually, it is an aggregate function’s companion.
  • 9.
  • 10.
    HAVING ▫ The substituteof WHERE for aggregate functions ▫ Usually, it is an aggregate function’s companion SELECT food, sum(sold) as totalSold FROM Foodchart group by food having sum(sold) > 450; 500hotdog totalSoldfood 70pizza06/06/13 500hotdog06/06/13 349pizza06/05/13 soldfooddate
  • 11.
    ORDER BY ▫ Sortthe result set in the order specified in the ORDER BY clause ▫ To sort columns from high to low or low to high , keyword ASC and DESC must be specified. ASC - Ascending, low to high DESC - Descending, high to low When ASC or DESC is used, it must be followed by the column name
  • 12.
    Example Select * From student Orderby stu_add ASC Stu_id Stu_name Stu_add 1 arpit adityapuram 3 abhishek D d nagar 2 bhupendra Tansen nagar
  • 13.
    WHERE VS HAVING •Similarities: ▫ The WHERE and HAVING clauses are both used to exclude records from the result set. • Differences ▫ WHERE clause  The WHERE clause is processed before the groups are created  Therefore, the WHERE clause can refer to any value in the original tables ▫ HAVING clause  The HAVING clause is processed after the groups are created  The HAVING clause CANNOT refer to individual columns from a table that are not also part of the group.