
- •Contents
- •Preface
- •Why Do We Need Databases?
- •What’s Up in the Kingdom?
- •A Database—That’s Our Solution!
- •Summary
- •Database Terms
- •Relational Databases
- •Types of Data Models
- •Data Extraction Operations
- •Questions
- •The Relational Database Prevails!
- •Summary
- •Answers
- •The E-R Model
- •Normalizing a Table
- •What Is the E-R Model?
- •How to Analyze the E-R Model
- •Normalizing a Table
- •Steps for Designing a Database
- •Answers
- •Using SQL
- •Searching for Data Using a SELECT Statement
- •Using Aggregate Functions
- •Joining Tables
- •Creating a Table
- •SQL Overview
- •Searching for Data Using a SELECT Statement
- •Creating Conditions
- •Aggregate Functions
- •Searching for Data
- •Joining Tables
- •Creating a Table
- •Summary
- •Answers
- •What Is a Transaction?
- •What Is a Lock?
- •Database Security
- •Speeding Things Up with Indexing
- •Disaster Recovery
- •Properties of Transactions
- •When Disaster Strikes
- •Indexes
- •Optimizing a Query
- •Summary
- •Answers
- •Databases in Use
- •Databases and the Web
- •Distributed Databases
- •Stored Procedures and Triggers
- •Databases on the Web
- •What Is a Distributed Database?
- •Partitioning Data
- •Preventing Inconsistencies with a Two-Phase Commit
- •Database Replication
- •Further Application of Databases
- •Summary
- •Answers
- •Closing Remarks
- •References
- •Index
- •About the Author
- •Updates
- •More Manga Guides

Searching for Data Using a SELECT Statement
|
Just ask the |
Please, |
|
We need to retrieve |
database to retrieve |
||
|
|||
the Product name |
|
||
only product names to |
|
||
column... |
|
||
create a product name |
|
||
|
|
||
list using SQL. |
|
|
|
|
from the |
|
|
|
Product Table. |
|
|
How do you do |
Mr. Database... |
|
|
that? |
|
|
Please retrieve the
Product name column...
You don’t need to
pray! Just use SQL...
You’d write this:
In SQL, one conversation is called a statement.
SELECT product_name
FROM product;
This SQL statement consists of two groups of words:
SELECT product_name and FROM product.
Let's Learn About SQL! 93

These groups of words are called phrases.
Product Table
Product code Product name Unit Price
From 101 Melon 800G
In SQL, you specify a column |
102 |
Strawberry |
150G |
|
name you want to retrieve |
103 |
Apple |
120G |
|
with the SELECT phrase and the |
||||
|
|
|
||
table name from which you |
104 |
Lemon |
200G |
|
want to retrieve it with the |
|
|
|
|
FROM phrase. |
|
Select |
|
|
|
|
|
||
Here is the retrieved |
|
Product name |
||
data. |
|
|
|
|
This allows you to |
|
Melon |
|
|
retrieve all product |
|
|
|
|
names from the |
|
Strawberry |
||
Product Table. |
|
|||
|
|
|
||
|
|
Apple |
|
|
Here you |
|
|
|
|
are! |
|
Lemon |
|
We are having a conversation with a database using SQL.
Various |
What about asking |
|
kinds... |
||
for a list of |
||
Hmm. |
||
products whose |
||
|
||
|
unit price is |
|
|
greater than or |
|
|
equal to 200G? |
That’s right. |
Well |
|
then, for |
||
You can retrieve |
||
example, |
||
necessary |
||
|
||
data by using |
|
|
various kinds |
|
|
of phrases. |
|
Greater than or equal to 200G
94 Chapter 4

|
|
In such cases, you |
In that case, you |
Yes, of |
specify conditions |
don’t want all the |
course. |
with the WHERE |
product data. |
|
phrase. |
|
|
For example, |
You only need |
|
|
to retrieve |
|
|
products whose |
|
|
unit price is |
|
|
greater than or |
|
|
equal to 200G. |
|
|
WHERE unit_price>=200 |
|
It is inconvenient |
|
|
|
to specify a |
No problem! |
|
|
column name each |
|
|
I see… |
time, isn't it? |
To specify all |
You write it |
|
columns, |
|
but... |
|
|
|
like this. |
|
|
|
|
It's a |
|
|
|
|
|
|
|
|
pain! |
|
|
|
Hmm... |
|
you can use *!
It can be summarized
as follows. Bang!!
SELECT *
FROM product
WHERE unit_price>=200
So,
this statement
retrieves all the data Here you are!
from the Product
Table...
Products that Cost 200G or More
Product |
Product |
Unit price |
|
code |
name |
||
|
|||
101 |
Melon |
800G |
|
104 |
Lemon |
200G |
that has a unit price of greater than or equal to 200G.

So, if you change the conditions, you can retrieve products whose unit price is below 200G.
WHERE unit_price<200
That’s right— like this!
Then, what do you do to retrieve apple, for example?
SELECT *
FROM product
WHERE product_name='apple';
If you do this, you can retrieve apple.
Now we need to learn how to make conditions.
Indeed...
Write it like this. When using characters as a condition, enclose them within quotation marks ( | ).
Product |
Product |
Unit |
code |
name |
price |
103 |
Apple |
120G |
Exactly.
96 Chapter 4

What about when |
What do you |
|
do in that |
||
you aren’t sure |
||
case? |
||
about the product |
||
|
||
name? |
|
You combine the word LIKE with a symbol.
Express the unknown
part using %, like this...
Melon
SELECT *
FROM product
WHERE product_name LIKE '%n';
This will retrieve product names that end with n.
Lemon
Product |
Product |
Unit |
code |
name |
price |
101 |
Melon |
800G |
104 |
Lemon |
200G |
Melon and Lemon |
That's convenient! |
are retrieved like |
|
that! |
|
|
Isn't it? |
Let's Learn About SQL! 97