05. Single Table Query - Sistem Basis Data

quiztugasujian 6 views 43 slides Jun 12, 2024
Slide 1
Slide 1 of 43
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43

About This Presentation

SBD


Slide Content

A Guide to SQL, Seventh Edition

Objectives
Retrieve data from a database using SQL
commands
Use compound conditions
Use computed columns
Use the SQL LIKE operator
Use the SQL IN operator
Sort data using the ORDER BY clause
A Guide to SQL, Seventh Edition

Objectives
Sort data using multiple keys and in ascending and
descending order
Use SQL aggregate functions
Use subqueries
Group data using the GROUP BY clause
Select individual groups of data using the HAVING
clause
Retrieve columns with null values
A Guide to SQL, Seventh Edition

Constructing Simple Queries
Important feature of DBMS is ability to answer a
wide variety of questions about the data
A query is a question represented in a way that
the DBMS can understand
Use the SELECT command to query a database
A Guide to SQL, Seventh Edition

Constructing Simple Queries
SELECT-FROM-WHERE is the basic form of the
command
SELECT clause is the list of columns to include in
query results
FROM clause is the name of the table with the
data being queried
WHERE clause is optional, listing any conditions
to apply to the data
A Guide to SQL, Seventh Edition

Retrieving Certain Columns and Rows
A command can retrieve specified columns and all
rows
List the number, name and balance of all
customers
No WHERE clause is needed, because all
customers are requested
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Retrieving All Columns and Rows
Use an asterisk (*) to indicate all columns in the
SELECT clause
Results will list all columns in the order in the
description when the table was created
List columns in SELECT clause to present
columns in a different order
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Using a WHERE Clause
WHERE clause is used to retrieve rows that
satisfy some condition
What is the name of customer number 148?
A simple conditionform: column name,
comparison operator, and then either a column
name or a value
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Using a WHERE Clause
Character values in SQL are case sensitive
“Grove” is different than “grove”
Simple conditions can compare columns
WHERE BALANCE > CREDIT_LIMIT
A Guide to SQL, Seventh Edition

Using Compound Conditions
Compound conditionsconnect two or more
simple conditions with AND, OR, and NOT
operators
AND operator shows results that all simple
conditions are true
OR operator shows results that any simple
condition is true
NOT operator reverses the truth of the original
condition
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Using the BETWEEN Operator
Not an essential feature in SQL
Same results can be obtained without it
Does make certain SELECT commands simpler to
construct
BETWEEN operator is inclusive
When using BETWEEN 2000 and 5000, values of
2000 or 5000 would be true
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Using Computed Columns
Computed columndoes not exist in the database
but is computed using data in existing columns
Computations can involve arithmetic operators
+ for addition
-for subtraction
* for multiplication
/ for division
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Using the LIKE Operator
Used to retrieve data where there may not be an
exact match using wildcards
LIKE %Central% will retrieve data with those
characters
“3829 Central” or “Centralia”
Underscore (_) represents any single character
“T_M” for TIM or TOM or T3M
A Guide to SQL, Seventh Edition

Using the IN Operator
IN operator allows for concise phrasing of certain
conditions
A Guide to SQL, Seventh Edition

Sorting
ORDER BY clause to list data in a specific order
Column on which data is to be sorted is the sort
key
Use ORDER BY clause followed by sort key
Rows are sorted in ascending order unless
another order is specified
A Guide to SQL, Seventh Edition

Additional Sorting Options
Possible to sort data by more than one key
Major sort keyand minor sort key
List sort keys in order of importance in the
ORDER BY clause
For descending order sort, use DESC
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Using Functions
Aggregate functionscalculate sums, averages,
counts, minimum and maximum values to groups
of rows
A Guide to SQL, Seventh Edition

Using the COUNT Function
Counts the number of rows in a table
Use of an asterisk allowed to represent any
column
A Guide to SQL, Seventh Edition

Using the SUM Function
Used to calculate totals of columns
Column to be summed must be specified and
must be numeric
AVG, MAX, and MIN functions are similar,
resulting in different statistics
Null values are ignored and not used in these
calculations
A Guide to SQL, Seventh Edition

Using the DISTINCT Operator
Used to ensure uniqueness in the data results
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Nesting Queries
Some queries will take two or more steps to obtain
desired results
A subqueryis an inner query placed inside
another query
Outer query can use results of the subquery to
find its results
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Grouping
Groupingcreates groups of rows that share
common characteristics
Calculations in the SELECT command are
performed for entire groups
Data can be GROUPED BYa particular column,
such as REP_NUM and then the statistics are
calculated
One line of output is produced for each group
A Guide to SQL, Seventh Edition

Using a HAVING Clause
The HAVING clause is used to restrict groups that
will be included
A Guide to SQL, Seventh Edition

Nulls
Sometimes a condition involves a column that can
be null
A Guide to SQL, Seventh Edition

A Guide to SQL, Seventh Edition

Summary
Create queries that retrieve data from single tables
using SELECT commands
Comparison operators: =, >,=>,<,=<, or <>, or !=
Compound conditions using AND,OR, and NOT
Use the BETWEEN operator
Use the LIKE operator
A Guide to SQL, Seventh Edition

Summary
Use the IN operator
Use and ORDER BY clause to sort data
Process aggregate functions with COUNT,
SUM,AVG,MAX, and MIN
Use the DISTINCT operator and subqueries
Use GROUP BY, HAVING and IS NULL
A Guide to SQL, Seventh Edition
Tags