SQL SQL (Structured Query Language) is a standard language for accessing and manipulating databases We have the following "student" table: SELECT * Example We want to select all the columns from the "student" table. The asterisk (*) is a quick way of selecting all columns. select * from student; An SQL SELECT Example Now we want to select the content of the columns named "class", "classno" and name from the student table. select class, classno, name from student; WHERE Clause Example Now we want to select only the persons studying in the class "1F" from the student table. select * from student where class='1f'; Page 1
Now we want to select only the persons with class number 1 from the student table. select * from student where classno=1; Now we want to select only the persons who pass Chinese from the student table. select * from student where chinese>=50; Now we want to select only the persons who pass english and maths from the student table. select * from student where english>=50 and maths>=50; Now we want to select only the persons who pass chinese or maths from the student table. select * from student where chinese>=50 or maths>=50; Page 2
ORDER BY Example Now we want to select all the persons from the table above, however, we want to sort the persons by their name. select * from student order by name; ORDER BY DESC Example Now we want to select all the persons from the table above, however, we want to sort the persons descending by their name. select * from student order by name desc; MAX() Example Now we want to find the highest mark of chinese from the student table select max(chinese) as highest_chinese from student; as highest_chinese is used to highest_chinese as the column name of the result. MIN() Example Now we want to find the lowest mark of maths from the student table select min(maths) as lowest_maths from student; Page 3
MIN() Example Now we want to find the lowest mark of maths from the student table select min(maths) as lowest_maths from student; SUM() Example Now we want to find the sum of all chinese fields. select sum(chinese) as total_chinese from student; AVG() Example Now we want to find the average mark of chinese. select avg(chinese) as average_chinese from student; Now we want to find the average mark of chinese in 1F class. select avg(chinese) as 1F_average_chinese from student where class='1f'; COUNT(column_name) Example Now we want to count the number of students who pass chinese" select count(name) as no_pass_chinese from student where chinese>=50; The result of the SQL statement above will be 4, because 4 students pass chinese. Page 4
Group by Example We can use group by to find sum(), min(), max(),count(),avg() according to a specific field. The following SQL command is used to display the average of Chinese mark for each class. select class, avg(chinese) from student group by class; The result is: class Expr1001 1A 73 1B 72 1C 55.5 2A 64 2B 71 The following SQL command is used to display the number of students whose math mark is greater than or equal to 50 for each class. select class, count(name) from student where Math>=50 group by class; The result is: class Expr1001 1A 2 1B 1 2A 1 2B 2 Reference: http://www.sqlzoo.net/ http://www.w3schools.com/sql/default.asp Page 5