sql select clause
DESCRIPTION
how to use the select clause in SQL with its full syntax...TRANSCRIPT
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.
ExampleSelect *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".
Exampleselect *from studentwhere 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 lowWhen ASC or DESC is used, it must be followed by the column name
ExampleSelect *From studentOrder 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.