Top Banner
PRESTIGE INSTITUTE OF MANAGEMENT GWALIOR Presented by- Arpit bhadoriya BCA 2 nd (B)
14

SQL select clause

Jun 14, 2015

Download

Technology

arpit bhadoriya

how to use the select clause in SQL with its full syntax...
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SQL select clause

PRESTIGE INSTITUTE OF MANAGEMENT

GWALIOR

Presented by- Arpit bhadoriya

BCA 2nd (B)

Page 2: SQL select clause

Syntax for SELECT statement

•Clauses must be written in the following order▫SELECT▫FROM▫WHERE▫GROUP BY▫HAVING▫ORDER BY

Page 3: SQL select clause

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.

Page 4: SQL select clause

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.

Page 5: SQL select clause

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”.

Page 6: SQL select clause

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".

Page 7: SQL select clause

Exampleselect *from studentwhere stu_id=2

Stu_id Stu_name Stu_add

2 Bhupendra Tansen nagar

Page 8: SQL select clause

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.

Page 9: SQL select clause

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

Page 10: SQL select clause

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

Page 11: SQL select clause

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

Page 12: SQL select clause

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

Page 13: SQL select clause

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.

Page 14: SQL select clause