SQL Strategy - A site to master SQL while executing it on the Web
Home >> SQL Strategy - Basics of SELECT statement (2)

Basics of SELECT statement (2)

You will learn how to use the WHERE clause to select records that match the criteria.

1. how to select records using the WEHRE clause

--Select records with SAL (salary) greater than 2000
SELECTENAME,JOB,SAL
FROMEMP
WHERESAL >= 2000
■Types of Comparison Operators

By placing a conditional expression after the WHERE clause, only records that match the condition can be selected. the comparison operators that can be used for the WHERE condition are

=The left-hand side is equal to the right-hand side
<left-hand side is smaller than right-hand side
<=the left side is less than the right side
>left-hand side is greater than right-hand side
>=the left side is greater than the right side
<>The left and right sides are not equal.

■Practice

Now for the Practice, I will give you two questions.

2. record selection by composite criteria

--Select employees whose JOB is 'CLERK' and whose SAL is 1000 or more
SELECTENAME,JOB,SAL
FROMEMP
WHEREJOB = 'CLERK' AND SAL >= 1000
■Types of logical operators

Use logical operators to select records by compound conditions.

ANDand
ORor
NOTnot

■How to use logical operators

If JOB = 'CLERK' AND SAL >= 1000, then the records that satisfy the conditions JOB = 'CLERK' and SAL >= 1000 are selected. To negate a conditional expression, put NOT in front of the expression, for example, NOT JOB = 'CLERK'.

■priority

Logical operators have precedence, and operations are performed in the order of NOT, AND, and OR. (parentheses) can be used to prioritize operations.

Now for the Practice, I will give you two questions.


3. past question with SELECT statements

Beginner System Administrator Morning Q35, 1995
The following table "Grades" contains the results of the English, Japanese, and mathematics examinations. Which of the following SQL statements is appropriate for the name of the student whose average score in the three subjects is 65 points or higher? Which one is appropriate?

Table grades
NoNameEnglishJapaneseMath
3011Sato567060
2023Suzuki706580
3047Tanaka807050
3066Nakamura708075

ASELECT No FROM grades
WHERE English > 65 OR Japanese > 65 OR Math > 65

BSELECT No FROM grades
WHERE English >= 65 AND Japanese >= 65 AND Math >= 65

CSELECT Name FROM grades
WHERE English >= 65 OR Japanese >= 65 OR Math >= 65

DSELECT Name FROM grades
WHERE English + Japanese + Math > 195

ESELECT Name FROM grades
WHERE English + Japanese + Math >= 195
Answer

E

Explanation

This problem should be solved as follows.

1. check the items to be displayed after SELECT.
Here we want "Student's Name", so A and B are No, so we remove them from the candidate list.。

2. Check if the WHERE condition is correct.
C is not good because it is selected if any of the scores are 65 or higher, even if the average score is not 65 or higher.

In the case of D, the student is selected if WHERE English + Japanese + Math > 195 and the average score is greater than 65.

I see that E is selected for students with WHERE English + Japanese + Math >= 195 and an average score of 65 or higher. Therefore, E is the correct answer.

This concludes [Basics of SELECT statement (2)].