0% found this document useful (0 votes)
40 views

SQL For Beginners

The document provides an overview of SQL for beginners, describing what SQL is, the basic components and structure of relational databases, and some common SQL queries including SELECT, WHERE, ORDER BY, JOIN, and aggregate functions. It explains tables, records, fields and keys in a database, and shows examples of SQL queries on sample data to retrieve, filter, sort and join information from multiple tables.

Uploaded by

graumanmoshe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

SQL For Beginners

The document provides an overview of SQL for beginners, describing what SQL is, the basic components and structure of relational databases, and some common SQL queries including SELECT, WHERE, ORDER BY, JOIN, and aggregate functions. It explains tables, records, fields and keys in a database, and shows examples of SQL queries on sample data to retrieve, filter, sort and join information from multiple tables.

Uploaded by

graumanmoshe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

SQL For Beginners

IEB IT Training Day 2008


Presented by Philip Norton
Roadmap
• What is SQL? A brief history
• Types of queries
• SELECT
– The Clauses
– Using SQL functions
– Using SQL aggregate queries
– Joining tables in SQL
• UPDATE, INSERT and DELETE
The Relational Database Model
• Proposed by E.F. Codd in 1970, while working for IBM
• Based on a formal mathematical theory... (yawn)

MyServer

School Weather

tblStaff tblPupils tblWeather

Name Title Subjects Name Grade Date Temp Rain


Tables, Columns and Rows
• A table (relation) is a collection of records
(tuples)
• A record is a set of values
• The values are defined by the columns
• Columns are also referred to as fields
• Rows are normally identified by a primary key
Example Data: tblPlayers
membNo firstName lastName age gender handicap membExpire clubID
1 John Smith 56 M 12 15/02/2005 1
2 Mary Smith 53 F 18 15/02/2005 1
3 Jeff Richardson 41 M 11 23/02/2009 2
4 John Gray 66 M 17 14/08/2010 1
5 Richard Jefferies 49 M 10 01/10/2008 1
6 Bill Brianston 40 M 5 04/06/2008 2
7 Delilah Sampson 45 F 4 16/01/2009 3
8 Jane Kirk 34 F 14 17/07/2008 3
9 Robert Cooke 56 M 9 23/11/2009 2
10 Sean Zimmer 63 M 7 18/12/2009 3
11 Miles Jones 62 M 10 26/01/2011 2
12 Sarah French 56 F 11 11/03/2008 1
Languages
• Structured Query Language (SQL, or
sometimes sequel) is common amongst
relational databases.
• Interact with the database
– Look up, add, delete, change
– Alter the structure of the database
– Alter security permissions
What is SQL?
• SQL is a language understood by many
databases
• DB vendors will implement a SQL interface for
their databases.
• SQL is a “layer of abstraction”
– Allows the user to specify WHAT they want
without having to worry about HOW
– HOW is specific to each database and its
implementation of SQL
A brief history of SQL
• Originated in IBM’s System R, one of the first
relational databases, developed in 1970s.
• Donald D. Chamberlin & Raymond F. Boyce
• Originally named Sequel, this name was
abandoned because of an existing trademark.
• The ANSI standard requires that SQL be
pronounced “es queue el” and not “see-quill”
• Designed to read clearly for non-programmers
Why SQL?
• Portability: change underlying DB with no
changes necessary to the program logic
• Interoperability: different DBMSs can
communicate through SQL, including
replication
• Reduced costs: Training is done once for all
databases
• Increased lifespan: standardised (ISO 9075)
languages last longer
What happens to the query?
• Query is sent to the DBMS
• Parsed
• Optimised
• Compiled
• The results are calculated
• A pointer to the results is returned
Query Types
• Security Manipulation
– Common in MySQL and other “real” databases
• Schema Manipulation
– Creating and making changes to tables
• Data Manipulation
– Finding, creating, changing and deleting data
Schema Manipulation
• CREATE
– Create a new table or view
• ALTER
– Change the structure of a table (fields)
• DROP
– Remove a table
• TRUNCATE
– Empty a table (MySQL, not Access)
Data Manipulation
• SELECT
– Get info from the DB
• INSERT
– Send new info to the DB
• UPDATE
– Change existing info in the DB
• DELETE
– Remove data from the DB
SELECT Queries
• Two functions:

1. Retrieve records
– “All the female golf players who are playing in
the tournament”
2. Retrieve summaries of the records:
– “What was the best score?”
Retrieving Info: SELECT
• SELECT queries allow us to choose records:
– What fields do we want to look at?
– What table(s) those fields come from?
– Restrictions based on field values
– Sorting the fields
– Limiting the number of rows
Some silly SQL
SELECT 5;

SELECT 5 + 5 * 5;

SELECT NOW();
Retrieving Info: SELECT / FROM
• Compulsory parts of the query: “WHAT” and
“WHERE”

SELECT <fields> FROM <table>;

• Fields  what
• Table  where
Retrieving Info: SELECT / FROM
• Get all the players’ last names:
SELECT lastName FROM tblPlayers;

• Get all the players’ first and last names:


SELECT firstName, lastName FROM
tblPlayers;

• Get all information about all players:


SELECT * FROM tblPlayers;
Retrieving Info: WHERE
• Restricting the records we are interested in.

SELECT <fields> FROM <table>


[ WHERE <condition> ];

• [ ]’s mean “optional part” and are not part


of the query!
• Conditions use fields and their values.
Retrieving Info: WHERE
• Get first and last names of any players who are
50 or over:
SELECT firstName, lastName FROM
tblPlayers WHERE age >= 50;

• Get all information of any players who are


Female and in their 40s:
SELECT * FROM tblPlayers WHERE gender
= ‘F’ AND age >= 40 AND age < 50;
Retrieving Info: WHERE
• Using literals
– Text must be included in quotes. Either “ or ‘…
• ‘Use single quotes in Java.’
• “Use double quotes in Delphi.”
– Dates enclosed in ##
• #22/02/2008#
– Numbers, as expected:
•4
• 5.6
Retrieving Info: WHERE
• Java:
sqlQuery = “SELECT * FROM
tblPlayers WHERE firstName =
‘John’;”;

• Delphi:
sqlQuery := ‘SELECT * FROM
tblPlayers WHERE firstName =
“John”;’;
Retrieving Info: WHERE / Comp
• Usual comparative operators:
– >, <, <=, >=
– “is equal to”  =
• (not ==)
– “not equal to”  <> or NOT field = value
• (not !=)
• A new one:
– “matches”  LIKE
Retrieving Info: WHERE / LIKE
• Get all the players whose first names start
with “J”:
SELECT * FROM tblPlayers WHERE
firstName LIKE ‘J*’;

• Get all the players whose last names end in


“on”:
SELECT * FROM tblPlayers WHERE
lastName LIKE ‘*on’;
Retrieving Info: ORDER BY
• Sorting the data in a specific order.

SELECT <fields> FROM <tables>


[ WHERE <condition> ]
[ ORDER BY <fields> ];

• If more than one field is provided, records that


have the same first field are then sorted by the
second field.
Retrieving Info: ORDER BY
• Get first and last names of any players who are 50
or over, sorted alphabetically by their last names:
SELECT firstName, lastName FROM
tblPlayers WHERE age >= 50 ORDER BY
lastName;

• Get all information of all players, sorted by age


(ascending) and then last name (alphabetically).
SELECT * FROM tblPlayers ORDER BY age,
lastName;
Retrieving Info: ORDER BY / DESC
• To sort information in the opposite order, use
the “DESC” modifier:

SELECT firstName, lastName FROM


tblPlayers ORDER BY age DESC;

SELECT * FROM tblPlayers WHERE age >= 60


ORDER BY membExpire DESC, lastName,
firstName;
Limiting the Output
• Who are the five best players?
• Who are the three oldest women?
Retrieving Info: Limiting output

SELECT [ TOP <x> ] <fields>


FROM <tables>
[ WHERE <condition> ]
[ ORDER BY <fields> ];

NB: MySQL is different: SELECT … FROM …


WHERE … ORDER BY … [ LIMIT <x> ]
Retrieving Info: Limiting output
SELECT TOP 5 firstName, lastName

FROM tblPlayers
ORDER BY handicap;

SELECT TOP 3 firstName, lastName

FROM tblPlayers
WHERE gender = ‘F’
ORDER BY age DESC;
Retrieving Info: Multiple Tables
• If we want to link information in tables, we
can do so using SQL and the JOIN sub-clause
• Remember we need to keep data separate to
keep our tables normalised
• JOINs allow us to “pretend” that the tables are
one big thing again
• Basic idea:
SELECT <what> FROM <where>
JOIN <how>;
tblCompetition

entryID membNo paid score


1 7 Yes 85
2 5 Yes 81
3 4 No 87
4 3 No 91
5 12 Yes 92
6 11 Yes 88
7 9 Yes 79
Joining: A first attempt
SELECT * FROM tblPlayers, tblCompetition

membNo firstName lastName

1 John Smith
2 Mary Smith
entryID membNo paid score
3 Jeff Richardson 1 7 Yes 85
4 John Gray 2 5 Yes 81
5 Richard Jefferies 3 4 No 87
6 Bill Brianston 4 3 No 91
7 Delilah Sampson 5 12 Yes 92
8 Jane Kirk 6 11 Yes 88
7 9 Yes 79
9 Robert Cooke
10 Sean Zimmer
11 Miles Jones
12 Sarah French
JOIN types
• INNER JOIN
– Each record with a foreign key has a
corresponding record in a different table
• Outer JOIN
– Either LEFT or RIGHT – depending which table gets
the “focus”.
– The focus table has all records listed, even if there
are no related records in the other table.
Competition Results
• How do we list the players with their scores and their
handicaps so that we can work out a winner?
• Data exists in different tables though…

lastName firstName score handicap


Sampson Delilah 85 4
Jeffries Richard 81 10
Gray John 87 17
Richardson Jeff 91 11
Etc.
INNER JOIN
SELECT lastName, firstName,
score, handicap
FROM tblPlayers
INNER JOIN tblCompetition
ON tblPlayers.membNo =
tblCompetition.membNo;
lastName firstName score handicap
Sampson Delilah 85 4
Jeffries Richard 81 10
Gray John 87 17
Richardson Jeff 91 11
Retrieving Info: Expressions
• Let’s make this query at least a little cleverer
• Can it tell us what their overall scores are?
• And sort the list in ascending order?

lastName firstName score handicap netScore


Sampson Delilah 85 4 81
Jeffries Richard 81 10 71
Gray John 87 17 70
Richardson Jeff 91 11 80
Etc.
INNER JOIN
SELECT lastName, firstName,
score, handicap, score -
handicap
FROM tblPlayers
INNER JOIN tblCompetition
ON tblPlayers.membNo =
tblCompetition.membNo
ORDER BY score – handicap;
Retrieving Info: Aliases
• OK, it works, but what’s with the field name?
• Can we rename it?
• finalScore?

lastName firstName score handicap score – handicap


Cooke Robert 79 9 70
Gray John 87 17 70
Jefferies Richard 81 10 71
Jones Miles 88 10 78
Richardson Jeff 91 11 80
French Sarah 92 11 81
Sampson Delilah 85 4 81
INNER JOIN
SELECT lastName, firstName,
score, handicap, score –
handicap AS finalScore
FROM tblPlayers
INNER JOIN tblCompetition
ON tblPlayers.membNo =
tblCompetition.membNo
ORDER BY score – handicap;
Aliases
• Sometimes queries can be simplified by giving
tables aliases (does not affect the query’s output):

SELECT lastName, firstName, score,


handicap, score – handicap AS
finalScore
FROM tblPlayers AS p
INNER JOIN tblCompetition AS c
ON p.membNo = c.membNo
ORDER BY score – handicap;
Multiple INNER JOINs
• Access SQL is “strange”...
• If you use many INNER JOINs in a query, you need
to surround each with brackets:

SELECT * FROM ( ( tblPlayers INNER


JOIN tblClubs ON tblPlayers.ClubID
= tblClubs.ClubID ) INNER JOIN
tblDesigners ON tblClubs.DesignID
= tblDesigners.DesignID );
Outer JOINs
• List all records from one table (at least once)
and any matching records in another.
• Not used very commonly.

• “List all the golfers in the club, and their


competition scores, if they played.”
Outer JOINs
SELECT * FROM tblPlayers LEFT JOIN
tblCompetition ON tblPlayers.membNo =
tblCompetition.membNo;
is the same as
SELECT * FROM tblCompetition RIGHT JOIN
tblPlayers ON tblPlayers.membNo =
tblCompetition.membNo;
membNo firstName lastName age gender handicap membExpire entryID membNo paid score
16John Smith 56 M 12 2005/02/15
17Mary Smith 53 F 18 2005/02/15
18Jeff Richardson 41 M 11 2009/02/23 12 18 FALSE 91
19John Gray 66 M 17 2010/08/14 11 19 FALSE 87
20Richard Jefferies 49 M 10 2008/10/01 10 20 TRUE 81
21Bill Brianston 40 M 5 2008/06/04
22Delilah Sampson 45 F 4 2009/01/16 9 22 TRUE 85
23Jane Kirk 34 F 14 2008/07/17
24Robert Cooke 56 M 9 2009/11/23 15 24 TRUE 79
25Sean Zimmer 63 M 7 2009/12/18
26Miles Jones 62 M 10 2011/01/26 14 26 TRUE 88
27Sarah French 56 F 11 2008/03/11 13 27 TRUE 92
Outer JOINs
• Can often be used to find records with no relations
in another table:

SELECT tblPlayers.*
FROM tblPlayers
LEFT JOIN tblCompetition ON
tblPlayers.membNo = age gender handicap membExpire
membNo firstName lastName
16 John Smith 56 M 12 2005/02/15
tblCompetition.membNo
17 Mary Smith 53 F 18 2005/02/15
21 Bill Brianston 40 M 5 2008/06/04
WHERE tblCompetition.membNo
23 Jane Kirk 34 F
IS NULL;
14 2008/07/17
25 Sean Zimmer 63 M 7 2009/12/18
Many to Many
• How many hours has each employee worked
on each project?

• SELECT * FROM ( ( assignments


INNER JOIN linktable ON
assignments.assignmentID =
linktable.assignmentID )
INNER JOIN projects ON linktable.projectID =
projects.projectsID )
Using Functions in Queries
SQL provides a number of simple functions that can be
used within the query:
• String handling
– LEFT, MID, RIGHT, LENGTH
• Mathematical
– ROUND, RND, INT
• Date / Time
– NOW, DAY, MONTH, YEAR, HOUR, MINUTE
• Logical
– IIF
Using Functions in Queries
SELECT * FROM tblPlayers WHERE
LENGTH(lastName) > 6;

SELECT * FROM tblPlayers WHERE


MONTH(membExpire) =
MONTH(NOW());

SELECT * FROM tblPlayers WHERE


(age + 1) MOD 10 = 0;
Using Functions in Queries
SELECT firstName, lastName,
YEAR(membExpire) AS ExpiryYear
FROM tblPlayers
WHERE handicap < 15 AND
membExpiry > NOW()
ORDER BY YEAR(membExpire) DESC,
lastName, firstName;
Using Functions in Queries
SELECT *, IIF(handicap <= 15, ‘Good’,
‘Bad’) AS rating
FROM tblPlayers;
rating membNo firstName lastName age gender handicap membExpire
Good 16 John Smith 56 M 12 2005/02/15
Bad 17 Mary Smith 53 F 18 2005/02/15
SELECT *,
Good 18 Jeff Richardson 41 M 11 2009/02/23
Bad 19 John Gray 66 M 17 2010/08/14
IIF(handicap <= 15, ‘Good’,
Good 20 Richard Jefferies 49 M 10 2008/10/01
Good 21 Bill Brianston 40 M 5 2008/06/04
IIF(handicap <= 25, ‘Average’,
Good 22 Delilah Sampson 45 F 4 2009/01/16
Good 23 Jane Kirk 34 F 14 2008/07/17
‘Bad’)
Good 24 Robert Cooke 56 M 9 2009/11/23
) AS rating
Good
Good
25
26
Sean
Miles
Zimmer
Jones
63
62
M
M
7
10
2009/12/18
2011/01/26
FROM tblPlayers;
Good 27 Sarah French 56 F 11 2008/03/11
Summarising Information
• Using Aggregate Functions
• Allow us to summarise information
• AVG, COUNT, MAX, MIN, SUM, STDEV, VAR
Aggregate Functions
SELECT AVG(age) FROM tblPlayers;

SELECT MIN(handicap) FROM tblPlayers;

SELECT MAX(handicap) FROM tblPlayers


WHERE gender = ‘M’;

SELECT COUNT(*) FROM tblPlayers WHERE


membExpire > CURDATE();
Aggregate Functions
• Consider these queries:

SELECT AVG(handicap) FROM


tblPlayers WHERE gender = ‘M’;

SELECT AVG(handicap) FROM


tblPlayers WHERE gender = ‘F’;
Aggregate Functions
• Or these queries:
SELECT COUNT(*) FROM tblPlayers
WHERE YEAR(membExpire) = 2005;
SELECT COUNT(*) FROM tblPlayers
WHERE YEAR(membExpire) = 2006;
SELECT COUNT(*) FROM tblPlayers
WHERE YEAR(membExpire) = 2007;
Etc…
Summarising by Groups
• These queries are requesting summarised
data for specific groups within the data set:
– Average handicap of men and women
– Number of players whose membership expired (or
is due to expire) each year
• We could create these queries
programmatically!
Summarising by Groups
for (int j = 2005; j <= 2010; j++)
{
String sql = “SELECT COUNT(*) “ +
“FROM tblPlayers WHERE “ +
“YEAR(membExpire) = ” + j +
“;”;
// run SQL query
}
Summarising by Groups
• But what happens in 20 years time?
• We will need to edit the code…
• Pain and drudgery...
• Is there an easier way?
Summarising Info: GROUP BY
Our query syntax becomes:

SELECT <fields> FROM <tables>


[ WHERE <condition> ]
[ GROUP BY <fields> ]
[ ORDER BY <fields> ];
Summarising Info: GROUP BY
• The two examples previously, with GROUP BY:

SELECT AVG(handicap) FROM


tblPlayers GROUP BY gender;

SELECT COUNT(*) FROM tblPlayers


GROUP BY YEAR(membExpire);
Summarising Info: GROUP BY
• SELECT MIN(handicap) AS
bestHandicap FROM tblPlayers
GROUP BY gender; bestHandicap
4
5

• SELECT gender, MIN(handicap) AS


bestHandicap FROM tblPlayers
GROUP BY gender; gender bestHandicap
F 4
M 5
Summarising Info: GROUP BY
• Using a WHERE with GROUP BY allows us to
restrict the records before they are
summarised.

SELECT COUNT(*) FROM tblPlayers


WHERE handicap >= 15 GROUP BY
YEAR(membExpire);
Summarising Info: GROUP BY
• If we group by a field, we must (should!)
include the field in the SELECT clause
• Gives meaning to the data.
• Grouping by a field makes that field a primary
key in the resulting data set, and as such should
be present.
• In a GROUP BY query, only Aggregate Functions
and GROUPed BY fields may be present in the
SELECT clause.
SELECT Syntax Becomes
Our query syntax becomes:

SELECT <fields> FROM <table>


[ INNER JOIN <table> ON <fields> ]
[ WHERE <condition> ]
[ GROUP BY <fields> ]
[ ORDER BY <fields> ];
Have you been paying attention?
SELECT gender, AVG(score –
handicap) AS averageScore
FROM tblPlayers
INNER JOIN tblCompetition ON
tblPlayers.membNo =
tblCompetition.membNo
GROUP BY gender;
Restricting the groups
• WHERE restricts records before the groups are
summarised.
• HAVING restricts summarised records after the
groups are summarised.
• HAVING conditions can involve the SELECTed
fields, and can include any aggregate
functions.
SELECT Syntax:
Our query syntax becomes:

SELECT <fields> FROM <table>


[ INNER JOIN <table> ON <fields> ]
[ WHERE <condition> ]
[ GROUP BY <fields>
[ HAVING <condition> ] ]
[ ORDER BY <fields> ];
Restricting the Groups
SELECT FLOOR(age / 10) * 10 AS
ageGroup, ROUND(AVG(handicap),
1) AS avgHandicap
FROM tblPlayers
WHERE gender = ‘M’
GROUP BY FLOOR(age / 10)
HAVING COUNT(*) >= 3;
The results…

ageGroup avgHandicap
40 8. 7
60 11.3

A tournament entry query: Finds the average


handicap in each age group (of 10 years) of men if
there are at least 3 members in that age group.
A Summary so far...
The complete SELECT syntax becomes:

SELECT [ TOP x ] <fields>


FROM <table>
[ INNER JOIN <table> ON <fields> ]
[ WHERE <condition> ]
[ GROUP BY <field>
[ HAVING <condition> ] ]
[ ORDER BY <fields> ];
Trying to remember this...
• Strapped • SELECT
• Teachers • TOP
• Find • FROM
• Jobs • (INNER) JOIN
• With • WHERE
• Gloriously • GROUP BY
• Higher • HAVING
• Offers • ORDER BY
There is still more:
• INSERT
– New rows into a table
• UPDATE
– Changing rows in a table
• DELETE
– Removing rows from a table

• Much simpler because they involve ONE table!


INSERT
INSERT INTO <table> ( <fields> )
VALUES ( <values> ), ...;

INSERT INTO tblPlayers (firstName,


lastName, age, gender, handicap,
membExpire)
VALUES (‘Douglas’, ‘Adams’, 42,
‘M’, 13, #22/02/2009#),
(‘Simon’, ‘Singh’, 40, ‘M’,
SQRT(121), NOW()+365);
UPDATE
UPDATE <table>
SET <field> = <value>...
WHERE <condition>;

• Remember the purpose of a primary key?


• PKs are very useful to limit changes to one
single row!
UPDATE
UPDATE tblPlayers SET handicap =
17 WHERE membNo = 2;
UPDATE tblPlayers SET age = age
+ 1;
UPDATE tblPlayers SET handicap =
handicap – 1, membExpire =
membExpire + 365
WHERE membExpire > NOW();
DELETE
DELETE FROM <table>
WHERE <condition>;

DELETE FROM tblPlayers WHERE


membExpire < NOW();
Which way is correct?
• These queries produce the same results.
Which one, though, is more correct?

SELECT * FROM tblPlayers WHERE


LEFT(lastName, 1) = ‘S’;

SELECT * FROM tblPlayers WHERE


lastName LIKE ‘S%’;
Readability
• Neither? Both?
• Many DBMS will optimised the queries into
the same thing anyway (which???)
• Remember that queries are there for us to
understand.
• The better query is the one that is follows
convention and is self-evident.
• There is always more than 1 way…
Bibliography
• SILBERSCHATZ, A., Korth, H.F., Sudarshan, S.,
2002. Database System Concepts. Mc Graw
Hill.
• VAN DER LANS, R.F., 2007. SQL for MySQL
Developers. Addison-Wesley.
• WIKIPEDIA CONTRIBUTORS, 11 February
2008. SQL. Wikipedia, The Free Encyclopaedia.
Accessed 16 February 2008.
Final Questions

Presented by Philip Norton


[email protected]

You might also like