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

T3_L1_SELECTSIMPLE

This document provides an introduction to SQL SELECT statements, focusing on extracting data from a single table using various clauses such as SELECT, WHERE, ORDER BY, and LIMIT. It explains the use of the AS keyword for aliasing fields, the IN and BETWEEN keywords for filtering, and includes examples of SQL queries. Additionally, it offers practice exercises for applying the concepts learned.

Uploaded by

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

T3_L1_SELECTSIMPLE

This document provides an introduction to SQL SELECT statements, focusing on extracting data from a single table using various clauses such as SELECT, WHERE, ORDER BY, and LIMIT. It explains the use of the AS keyword for aliasing fields, the IN and BETWEEN keywords for filtering, and includes examples of SQL queries. Additionally, it offers practice exercises for applying the concepts learned.

Uploaded by

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

Introduction to SQL SELECT

Topic 3
Lesson 1 – SQL SELECT on one table
Chapter 3 Murach’s MySQL

2
Extracting data from a single table
SELECT field_list
FROM table
WHERE condition_true
ORDER BY order_field_list
LIMIT num_tuples;
id name school credits_earned credits_req
1 Smith Khoury 32 120

2 Shah D’Amore 64 128


McKim
3 Li Khoury 50 120

The field_list and the order_field_list are comma separated


All SQL commands are terminated with a semicolon

3
AS keyword

The AS keyword in the field list allows us to change the


name of the attribute in the results.
Student

id name school credits_earned credits_req


1 Smith Khoury 32 120

2 Shah D’Amore 64 128


McKim
3 Li Khoury 50 120

Write a query that returns the school column from the


Student table with the field school aliased as
school_name

4
Computed result and the AS keyword

Your field list can contain a computation on the fields in


the table
Student

id name school credits_earned credits_req


1 Smith Khoury 32 120

2 Shah D’Amore 64 128


McKim
3 Li Khoury 50 120

Write a query that returns the ID, Name and the number
of credits left for the student to graduate

5
ORDER BY clause
You can specify multiple fields to order by as well as different
ordering directions. ASC – signifies ORDER BY with values
increasing. DESC – signified ORDER BY with values
decreasing.

Extended format:
ORDER BY expression [ASC|DESC][, expression
[ASC|DESC]]

6
Ordering by field position
Rather than specifying the name of the field in the output, you
can specify the field position in the output. This places a
dependency between the field list and the order_by list.

This functionality is provided in SQL but typically is


considered bad practice.

7
Limit clause

Limit the number of tuples that are returned as the result. The
default is to return the first n tuples in the result. But you can
also specify an offset to start returning from. The extended
syntax for the clause is:
LIMIT [offset,] row_count

EXAMPLE:
SELECT * FROM STUDENT LIMIT 2, 2;

8
WHERE clause
The format of the WHERE clause is:
expression1 operation expression2

The supported operations are: = equals, < less than, > greater
than, <= less than or equal, >= greater than or equal, <> not
equal , != not equal, LIKE (a limited regular expression) .
LIKE has 2 special characters The under score matches any
one character, % matches any collection of characters.

Example: SELECT * FROM student where ID != 1;


SELECT * FROM student where Name LIKE ‘D%’;

9
WHERE clause allows multiple conditions

The WHERE clause uses the KEYWORDS AND and OR


to place multiple restrictions on the data
Student_instance1

id name school credits_earned credits_req


1 Smith Khoury 32 120

2 Shah D’Amore 64 128


McKim
3 Li Khoury 50 120

The syntax for the compound WHERE clause is


WHERE [NOT] search_condition_1 {AND|OR}
[NOT] search_condition_2 ...

10
IN Keyword

IN operation returns true if the left side is equal to any


value on the right side. The IN keyword can be
preceded by the NOT keyword
EXAMPLE:
SELECT Name FROM Student where ID IN (1,2);
SELECT Name FROM Student where ID NOT IN
(1,2);

id name school credits_earned credits_req


1 Smith Khoury 32 120

2 Shah D’Amore 64 128


McKim
3 Li Khoury 50 120

11
BETWEEN Keyword

Between operation specifies a range for a condition


EXAMPLE:
SELECT name FROM student where Name between
“S” and “Szzz”;
SELECT name FROM student where id BETWEEN 1
AND 5;
id name school credits_earned credits_req
1 Smith Khoury 32 120

2 Shah D’Amore 64 128


McKim
3 Li Khoury 50 120

12
LIKE Keyword

LIKE operation allows you to match parts of a string


EXAMPLE:
SELECT name FROM student where name LIKE
“S%”
SELECT name FROM student where name LIKE
“%s”;
id name school credits_earned credits_req
1 Smith Khoury 32 120

2 Shah D’Amore 64 128


McKim
3 Li Khoury 50 120

13
SQL Practice (1)
Write SQL statements to :
1. Extract all student names from the student table
2. Extract all Khoury college students from the student table
3. Extract all Khoury college students from the student table
ordering the results by the last name
4. Extract 5 Khoury college students from the student table
ordering the results by the last name

14
SQL Practice (2)
Write SQL statements to :
1. Extract all fields from the student table where the id is
either 1 or 3
2. Extract all Khoury college students and all D’Amore
McKim students
3. Extract all college students whose IDs are < 15 OR >20
4. Extract college students from the student table whose IDs
are greater than 15 and less than 20
id name school credits_earned credits_req
1 Smith Khoury 32 120

2 Shah D’Amore 64 128


McKim
3 Li Khoury 50 120

15
MySQL work
Let’s use the MySQL workbench to write SELECT statements
using the ap database

Go to the website, page topic 2, and download the


sql_exercises1.sql exercise. Complete the first 9 queries.

16
SQL Practice (3)
1. Extract all information on students, first order the output by
credits_earned in ascending order and then order by
name in ascending order
2. Extract all information on students, first order the output by
credits_earned in descending order and then order by
name in ascending order.

17
Summary
In this module you learned:
• SQL SELECT clauses: SELECT FROM, WHERE,
ORDER BY, LIMIT.

Relational Data Model 18

You might also like