Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL.

Similar presentations


Presentation on theme: "SQL."— Presentation transcript:

1 SQL

2 Instructional Objective
To make students aware of SQL commands. To make them understand different syntax of commands according to condition. To make them aware of extracting specified information from a table using query.

3 Learning Objective After studying this lesson the students will be able to Working of most of the select statement commands. -State the syntax of making the commands

4 Pre-Requisites -Students will aware of how to make a table. i.e. create command They know insert the data into tables.

5 Introduction to SQL Concept of SQL
The user specifies a certain condition. The program will go through all the records in the database file and select those records that satisfy the condition.(searching). The result of the query will then be stored in form of a table.

6 Basic structure of an SQL query

7 The Situation: Student Particulars
field type width contents id numeric student id number name character name dob date date of birth sex character sex: M / F class character class hcode character house code: R, Y, B, G dcode character district code remission logical fee remission mtest numeric Math test score

8 Table Structure

9 I General Structure SELECT ...... FROM ...... WHERE ......
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ; FROM table name WHERE condition

10 I General Structure SELECT * FROM student
eg. 1 List all the student records. SELECT * FROM student Result

11 I General Structure SELECT name, hcode FROM student WHERE class="1A“;
eg. 2 List the names and house code of 1A students. SELECT name, hcode FROM student WHERE class="1A“; Class 1A 1B : Class 1A 1B : class="1A"

12 I General Structure eg. 2 List the names and house code of 1A students. Result

13 I General Structure SELECT DISTINCT dcode FROM student
eg. 3 List the residential district of the Red House members. SELECT DISTINCT dcode FROM student WHERE hcode="R“; Result

14 I 1B Girls ? General Structure
eg. 4 List the names and dob of 1B girls. 1B Girls ?

15 I Condition for "1B Girls": 1) class = "1B" 2) sex = "F"
General Structure eg. 4 List the names and dob of 1B girls. Condition for "1B Girls": 1) class = "1B" 2) sex = "F" 3) Both ( AND operator)

16 I General Structure SELECT name, dob
eg. 4 List the names and ages (1 d.p.) of 1B girls. SELECT name, dob FROM student WHERE class="1B" AND sex="F“;

17 II Comparison SELECT name, mtest FROM student ; WHERE class="1A" AND ;
eg. 5 List the 1A students whose Math test score is between 80 and 90 (incl.) SELECT name, mtest FROM student ; WHERE class="1A" AND ; mtest BETWEEN 80 AND 90 Result

18 II Comparison SELECT name, class FROM student ; WHERE name LIKE "T%"
eg. 6 List the students whose names start with "T". SELECT name, class FROM student ; WHERE name LIKE "T%" Result

19 II Comparison SELECT name, class, hcode FROM student
eg. 7 List the Red house members whose names contain "a" as the 2nd letter. SELECT name, class, hcode FROM student WHERE name LIKE "_a%" AND hcode="R“; Result

20 III Grouping SELECT ...... FROM ...... WHERE condition ;
GROUP BY groupexpr [HAVING requirement] Group functions: COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) – groupexpr specifies the related rows to be grouped as one entry. Usually it is a column. – WHERE condition specifies the condition of individual rows before the rows are group. HAVING requirement specifies the condition involving the whole group.

21 III Grouping eg. 8 List the number of students of each class.

22 1A 1A 1B 1C 1B 1C Group By Class COUNT( ) COUNT( ) COUNT( ) class 1A
Student class 1A 1B 1C 1A 1A 1B 1C COUNT( ) COUNT( ) 1B COUNT( ) 1C

23 III Grouping SELECT class, COUNT(*) FROM student ; GROUP BY class
eg. 8 List the number of students of each class. SELECT class, COUNT(*) FROM student ; GROUP BY class Result

24 III Grouping eg. 10 List the average Math test score of each class.

25 Group By Class Student class 1A 1B 1C 1A 1B 1C AVG( )

26 III Grouping SELECT class, AVG(mtest) FROM student ; GROUP BY class
eg. 12 List the average Math test score of each class. SELECT class, AVG(mtest) FROM student ; GROUP BY class Result

27 III Grouping SELECT dcode, COUNT(*) FROM student ;
eg. 13 List the number of girls of each district. SELECT dcode, COUNT(*) FROM student ; WHERE sex="F" GROUP BY dcode Result

28 III Grouping SELECT MAX(mtest), MIN(mtest), dcode ; FROM student ;
eg. 14 List the max. and min. test score of Form 1 students of each district. SELECT MAX(mtest), MIN(mtest), dcode ; FROM student ; WHERE class LIKE "1_" GROUP BY dcode Result

29 III Grouping SELECT AVG(mtest), class FROM student ;
eg. 15 List the average Math test score of the boys in each class. The list should not contain class with less than 3 boys. SELECT AVG(mtest), class FROM student ; WHERE sex="M" GROUP BY class ; HAVING COUNT(*) >= 3 Result

30 IV Display Order SELECT ...... FROM ...... WHERE ......
GROUP BY ; ORDER BY colname ASC / DESC

31 IV Display Order SELECT name, id FROM student ;
eg. 16 List the boys of class 1A, order by their names. SELECT name, id FROM student ; WHERE sex="M" AND class="1A" ORDER BY name Result ORDER BY dcode

32 IV Display Order SELECT name, id, class, dcode FROM student ;
eg. 17 List the 2A students by their residential district. SELECT name, id, class, dcode FROM student ; WHERE class="2A" ORDER BY dcode Result

33 IV Display Order (in desc. order).
eg. 18 List the number of students of each district (in desc. order). SELECT COUNT(*) AS cnt, dcode FROM student ; GROUP BY dcode ORDER BY cnt DESC Result

34 IV Display Order SELECT name, class, hcode FROM student ;
eg. 19 List the boys of each house order by the classes. (2-level ordering) SELECT name, class, hcode FROM student ; WHERE sex="M" ORDER BY hcode, class

35 IV Display Order Order by class Order by hcode Result : Blue House
Green House :


Download ppt "SQL."

Similar presentations


Ads by Google