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

Select From Checks Select From Checks ?: The Check Numbers and The Remarks

The document provides information on expressions, conditions, and operators in SQL. It defines expressions as anything that returns a value, and conditions as elements contained in the WHERE clause used to filter query results. It describes the basic types of operators - arithmetic, comparison, character, logical, set, and miscellaneous - and provides examples of using the plus, minus, and other arithmetic operators to manipulate values in queries.

Uploaded by

vinitajenny
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Select From Checks Select From Checks ?: The Check Numbers and The Remarks

The document provides information on expressions, conditions, and operators in SQL. It defines expressions as anything that returns a value, and conditions as elements contained in the WHERE clause used to filter query results. It describes the basic types of operators - arithmetic, comparison, character, logical, set, and miscellaneous - and provides examples of using the plus, minus, and other arithmetic operators to manipulate values in queries.

Uploaded by

vinitajenny
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

Quiz

1. Do the following statements return the same or different output:

SELECT * FROM CHECKS;


select * from checks;?

2. The following queries do not work. Why not?

a. Select *

b. Select * from checks

c. Select amount name payee FROM checks;

3. Which of the following SQL statements will work?

a . select *

from checks;

b. select * from checks;

c. select * from checks

Exercises
1. Using the CHECKS table from earlier today, write a query to return just
the
check numbers and the remarks.

2. Rewrite the query from exercise 1 so that the remarks will appear as the first
column in your query results.

3. Using the CHECKS table, write a query to return all the unique remarks.
Expressions, Conditions, and Operators

Objectives
Introduction to the Query: The SELECT Statement," you used SELECT and
FROM to manipulate data in interesting (and useful) ways. Today you learn more
about SELECT and FROM and expand the basic query with some new terms to go
with query, table, and row, as well as a new clause and a group of handy items called
operators.

In this chapter guys you’ll learn…

Know what an expression is and how to use it


q

Know what a condition is and how to use it


q

Be familiar with the basic uses of the WHERE clause


q

Be able to use arithmetic, comparison, character, logical, and set operators


q

Have a working knowledge of some miscellaneous operators


q

Expressions
The definition of an expression is simple: An expression returns a value. Expression
types are very broad, covering different data types such as String, Numeric, and
Boolean. In fact, pretty much anything following a clause ( SELECT or FROM , for
example) is an expression. In the following example amount is an expression that
returns the value contained in the amount column.
SELECT amount FROM checks;

In the following statement NAME, ADDRESS, PHONE and ADDRESSBOOK are


expressions:

SELECT NAME, ADDRESS, PHONE


FROM ADDRESSBOOK;

Now, examine the following expression:

WHERE NAME = 'BROWN'

It contains a condition, NAME = 'BROWN' , which is an example of a Boolean


expression.
NAME = 'BROWN' will be either TRUE or FALSE , depending on the condition
=.

Conditions
If you ever want to find a particular item or group of items in your database, you need
one or more conditions. Conditions are contained in the WHERE clause. In the
preceding example, the condition is

NAME = 'BROWN'

To find everyone in your organization who worked more than 100 hours last month,
your condition would be

NUMBEROFHOURS > 100

Conditions enable you to make selective queries. In their most common form,
conditions comprise a variable, a constant, and a comparison operator. In the first
example the variable is NAME , the constant is 'BROWN' , and the comparison
operator is = . In the second example the variable is NUMBEROFHOURS , the
constant is 100 , and the comparison operator is > . You need to know about two more
elements before you can write conditional queries: the WHERE clause and operators.
The WHERE Clause
The syntax of the WHERE clause is

SYNTAX:

WHERE <SEARCH CONDITION>

SELECT , FROM , and WHERE are the three most frequently used clauses in SQL.
WHERE simply causes your queries to be more selective. Without the WHERE
clause, the most useful thing you could do with a query is display all records in the
selected table(s).

For example:

INPUT:

SQL> SELECT * FROM BIKES;

lists all rows of data in the table BIKES .

OUTPUT:

NAME FRAMESIZE COMPOSITION MILESRIDDEN TYPE

TREK 2300 22.5 CARBON FIBER 3500 RACING


BURLEY 22 STEEL 2000 TANDEM
GIANT 19 STEEL 1500 COMMUTER
FUJI 20 STEEL 500 TOURING
SPECIALIZED 16 STEEL 100 MOUNTAIN
CANNONDALE 22.5 ALUMINUM 3000 RACING

6 rows selected.
If you wanted a particular bike, you could type

INPUT/OUTPUT:

SQL> SELECT *
FROM BIKES WHERE NAME = 'BURLEY';

which would yield only one record:

NAME FRAMESIZE COMPOSITION MILESRIDDEN TYPE

BURLEY 22 STEEL 2000 TANDEM

ANALYSIS:

This simple example shows how you can place a condition on the data that you want to
retrieve.
Operators
Operators are the elements you use inside an expression to articulate how you want
specified conditions to retrieve data. Operators fall into six groups: arithmetic,
comparison, character, logical, set, and miscellaneous.

Arithmetic Operators
The arithmetic operators are plus ( + ), minus (-), divide ( / ), multiply ( * ), and modulo
( % ).
The first four are selfexplanatory. Modulo returns the integer remainder of a division.
Here are two examples:

5%2=1
6%2=0

The modulo operator does not work with data types that have decimals, such as Real or
Number.
If you place several of these arithmetic operators in an expression without any
parentheses, the operators are resolved in this order: multiplication, division, modulo,
addition, and subtraction. For example, the expression

2*6+9/3

equals

12 + 3 = 15

However, the expression

2 * (6 + 9) / 3

equals

2 * 15 / 3 = 10

Watch where you put those parentheses! Sometimes the expression does exactly what
you tell it to do, rather than what you want it to do.

The following sections examine the arithmetic operators in some detail and give you a
chance to write some queries.

Plus (+)

You can use the plus sign in several ways. Type the following statement to display the
PRICE table:

INPUT:

SQL> SELECT * FROM PRICE;


OUTPUT:

ITEM WHOLESALE

TOMATOES .34
POTATOES .51
BANANAS .67
TURNIPS .45
CHEESE .89
APPLES .23

6 rows selected.

Now type:

INPUT/OUTPUT:

SQL> SELECT ITEM, WHOLESALE, WHOLESALE + 0.15


FROM PRICE;

Here the + adds 15 cents to each price to produce the following:

ITEM WHOLESALE WHOLESALE+0.15

TOMATOES .34 .49


POTATOES .51 .66
BANANAS .67 .82
TURNIPS .45 .60
CHEESE .89 1.04
APPLES .23 .38

6 rows selected.

ANALYSIS:

What is this last column with the unattractive column heading WHOLESALE+0.15 ?
It's not in the original table. (Remember, you used * in the SELECT clause, which
causes all the columns to be shown.) SQL allows you to create a virtual or derived
column by combining or modifying existing columns.
Retype the original entry:

INPUT/OUTPUT:

SQL> SELECT * FROM PRICE;

The following table results:

ITEM WHOLESALE

TOMATOES .34
POTATOES .51
BANANAS .67
TURNIPS .45
CHEESE .89
APPLES .23

6 rows selected.

ANALYSIS:

The output confirms that the original data has not been changed and that the column
heading WHOLESALE+0.15 is not a permanent part of it. In fact, the column
heading is so unattractive that you should do something about it.

Type the following:


INPUT/OUTPUT:

SQL> SELECT ITEM, WHOLESALE, (WHOLESALE + 0.15) RETAIL FROM PRICE;


ITEM WHOLESALE RETAIL

TOMATOES .34 .49


POTATOES .51 .66
BANANAS .67 .82
TURNIPS .45 .60
CHEESE .89 1.04
APPLES .23 .38

6 rows selected.
ANALYSIS:

This is wonderful! Not only can you create new columns, but you can also rename them
on the fly. You can rename any of the columns using the syntax column_name alias
(note the space between column_name and alias ).

For example, the query

INPUT/OUTPUT:

SQL> SELECT ITEM PRODUCE, WHOLESALE, WHOLESALE + 0.25 RETAIL


FROM PRICE;

renames the columns as follows:

PRODUCE WHOLESALE RETAIL

TOMATOES .34 .59


POTATOES .51 .76
BANANAS .67 .92
TURNIPS .45 .70
CHEESE .89 1.14
APPLES .23 .48

NOTE: Some implementations of SQL use the syntax <column name =


alias> . The preceding example would be written as follows:

SQL> SELECT ITEM = PRODUCE,


WHOLESALE, WHOLESALE + 0.25 = RETAIL FROM PRICE;

Check your implementation for the exact syntax.


You might be wondering what use aliasing is if you are not using command-line SQL.
Fair enough. Have you ever wondered how report builders work? Someday, when you
are asked to write a report generator, you'll remember this and not spend weeks
reinventing what Dr. Codd and IBM have wrought.

So far, you have seen two uses of the plus sign. The first instance was the use of the
plus sign in the SELECT clause to perform a calculation on the data and display the
calculation. The second use of the plus sign is in the WHERE clause. Using operators
in the WHERE clause gives you more flexibility when you specify conditions for
retrieving data.

In some implementations of SQL, the plus sign does double duty as a character
operator.
You'll see that side of the plus a little later today.

Minus (-)

Minus also has two uses. First, it can change the sign of a number. You can use the table
HILOW to demonstrate this function.

INPUT:

SQL> SELECT * FROM HILOW;

OUTPUT:

STATE HIGHTEMP LOWTEMP

CA -50 120
FL 20 110
LA 15 99
ND -70 101
NE -60 100

For example, here's a way to manipulate the data:

INPUT/OUTPUT:

SQL> SELECT STATE, -HIGHTEMP LOWS, -LOWTEMP HIGHS


FROM HILOW;
STATE LOWS HIGHS

CA 50 -120
FL -20 -110
LA -15 -99
ND 70 -101
NE 60 -100

The second (and obvious) use of the minus sign is to subtract one column from another.
For example:

INPUT/OUTPUT:

SQL> SELECT STATE,


2 HIGHTEMP LOWS,
3 LOWTEMP HIGHS,
4 (LOWTEMP - HIGHTEMP) DIFFERENCE
5 FROM HILOW;

STATE LOWS HIGHS DIFFERENCE

CA -50 120 170


FL 20 110 90
LA 15 99 84
ND -70 101 171
NE -60 100 160

Notice the use of aliases to fix the data that was entered incorrectly. This remedy is
merely a temporary patch, though, and not a permanent fix. You should see to it that
the data is corrected and entered correctly in the future

This query not only fixed (at least visually) the incorrect data but also created a new
column containing the difference between the highs and lows of each state.

If you accidentally use the minus sign on a character field, you get something like this:

INPUT/OUTPUT:

SQL> SELECT -STATE FROM HILOW;


ERROR:
ORA-01722: invalid number
no rows selected

The exact error message varies with implementation, but the result is the same.
Divide (/)

The division operator has only the one obvious meaning. Using the table PRICE , type
the following:

INPUT:

SQL> SELECT * FROM PRICE;

OUTPUT:

ITEM WHOLESALE

TOMATOES .34
POTATOES .51
BANANAS .67
TURNIPS .45
CHEESE .89
APPLES .23

6 rows selected.

You can show the effects of a two-for-one sale by typing the next statement:

INPUT/OUTPUT:

SQL> SELECT ITEM, WHOLESALE, (WHOLESALE/2) SALEPRICE


2 FROM PRICE;

ITEM WHOLESALE SALEPRICE

TOMATOES .34 .17


POTATOES .51 .255
BANANAS .67 .335
TURNIPS .45 .225
CHEESE .89 .445
APPLES .23 .115
6 rows selected.
The use of division in the preceding SELECT statement is straightforward (except
that coming up with half pennies can be tough).

Multiply (*)

The multiplication operator is also straightforward. Again, using the PRICE table,
type the following:

INPUT:

SQL> SELECT * FROM PRICE;

OUTPUT:

ITEM WHOLESALE

TOMATOES .34
POTATOES .51
BANANAS .67
TURNIPS .45
CHEESE .89
APPLES .23

6 rows selected.
This query changes the table to reflect an across-the-board 10 percent discount:

INPUT/OUTPUT:

SQL> SELECT ITEM, WHOLESALE, WHOLESALE * 0.9 NEWPRICE FROM PRICE;

ITEM WHOLESALE NEWPRICE

TOMATOES .34 .306


POTATOES .51 .459
BANANAS .67 .603
TURNIPS .45 .405
CHEESE .89 .801
APPLES .23 .207
6 rows selected.
These operators enable you to perform powerful calculations in a SELECT statement.

Modulo (%)

The modulo operator returns the integer remainder of the division operation. Using the
table REMAINS , type the following:

INPUT/OUTPUT:

SQL> SELECT * FROM REMAINS;

NUMERATOR DENOMINATOR

10 5
8 3
23 9
40 17
1024 16
85 34

6 rows selected.

You can also create a new column, REMAINDER, to hold the values of
NUMERATOR % DENOMINATOR :

INPUT/OUTPUT:

SQL> SELECT NUMERATOR, DENOMINATOR, NUMERATOR%DENOMINATOR


REMAINDER FROM REMAINS;

NUMERATOR DENOMINATOR REMAINDER

10 5 0
8 3 2
23 9 5
40 17 6
1024 16 0
85 34 17
6 rows selected.
The following statement produces results that are identical to the results in the
preceding statement:

SQL> SELECT NUMERATOR,


DENOMINATOR, MOD(NUMERATOR,DENOMINATOR) REMAINDER
FROM REMAINS;

Precedence

This section examines the use of precedence in a SELECT statement. Using the
database PRECEDENCE , type the following:
SQL> SELECT * FROM PRECEDENCE;

N1 N2 N3 N4

1 2 3 4
13 24 35 46
9 3 23 5
63 2 45 3
7 2 1 4

Use the following code segment to test precedence:

INPUT/OUTPUT:

SQL> SELECT
2 N1+N2*N3/N4,
3 (N1+N2)*N3/N4,
4 N1+(N2*N3)/N4
5 FROM PRECEDENCE;

N1+N2*N3/N4 (N1+N2)*N3/N4 N1+(N2*N3)/N4

2.5 2.25 2.5


31.26087 28.152174 31.26087
22.8 55.2 22.8
93 975 93
7.5 2.25 7.5
Notice that the first and last columns are identical. If you added a fourth column
N1+N2* (N3/N4) , its values would also be identical to those of the current first and
last columns.

You might also like