Escolar Documentos
Profissional Documentos
Cultura Documentos
What is SQL?
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
• SQL is an ANSI (American National Standards
Institute) standard
What Can SQL do?
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and
views
Types of SQL
• Data Definition Language (DDL)
– CREATE
– ALTER
– DROP
• DML Data Manipulation Language (DML)
– INSERT
– UPDATE
– DELETE
– SELECT
DML
Command Purpose
condition can be :
(1) an inequality, or
(2) a string comparison
using logical operators AND, OR, NOT.
Logical Operators in Condition
Symbol Purpose
= Equality
<> Inequality
> Greater than
< Less than
>= Greater equal
<= Less equal
AND If all conditions are true
OR If any condition is true
NOT Negation
BETWEEN Between an inclusive range
IN To specify multiple possible values for a column
LIKE Search for a pattern
FREETEXT Search for free text (i.e. I am eating potato) it will return
the results of each word.
Example
SELECT name, hcode, class FROM student
WHERE class="1A"
Class Class
1A 1A
1A
class="1A" 1A
1A 1A
1B 1B
1B 1B
: :
Example
Result
name class
Tobe 1B
Teddy 1B
Tim 2A
Comparison
Group functions:
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
1A 1A
1A
COUNT( )
1B
1B
1B
1B 1B
COUNT( )
1B
1B
1C
1C 1C
1C
COUNT( )
Student
Grouping
eg. 11 List the number of students of each class.
SELECT class, COUNT(*) FROM student
GROUP BY class
class cnt
Result 1A 10
1B 9
1C 9
2A 8
2B 8
2C 6
Grouping
eg. 12 List the average Math test score of each class.
Group By Class
class
1A
1A 1A
1A
AVG( )
1B
1B
1B
1B
AVG( )
1B
1B
1B
1C
1C 1C AVG( )
1C
Student
Grouping
eg. 12 List the average Math test score of each class.
class avg_mtest
Result 1A 85.90
1B 70.33
1C 37.89
2A 89.38
2B 53.13
2C 32.67
Grouping
eg. 13 List the number of girls of each district.