Database Management System 1: SELECT Statement
Database Management System 1: SELECT Statement
SELECT Statement
Lesson Objective
• Where:
o A SELECT clause, which specifies the columns to be displayed
o A FROM clause, which identifies the table containing the
columns that are listed in the SELECT clause
Select (ALL) column
• You can display all rows and column in a table
by using the SELECT followed by (*) asterisk.
• (*) is used to call/display all row in the column.
• Example:
SELECT * FROM AUTHORS
Select SPECIFIC row
• Specifically display a column by using the SELECT statement then separate
with commas.
• In the SELECT clause, specify the columns that you want to be displayed in
the order in which you want them to appear in the output.
• For example, to display the last name before the first name of author in
AUTHORS table use the following example
• Example:
SELECT LNAME, FNAME
FROM AUTHORS;
Writing SQL Statements guidelines
• SQL statements are not case sensitive, a user may use all capital letter
combination of small or uppercase.
• SQL statements can be entered on one or many lines to make the code
readable.
• Keywords cannot be split across lines or abbreviated. Example: SELECT should
be call ad SELECT not SE LECT
• Clauses are usually placed on separate lines for readability and ease of editing
and to make code readable.
• Indents should be used to make code more readable.
• Keywords typically are entered in uppercase; all other words, such as table
names and columns names are entered in lowercase but is not required.
Arithmetic Expressions
• Create expression with number and date values.
• An arithmetic expression can contain column names,
constant numeric values, and the arithmetic
operators.
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
Arithmetic Expressions cont.
• Example:
SELECT LNAME, FNAME, BOOK, YR_PUB + 5
FROM AUTHORS;
Rules of Precedence
• If all operators are present, multiplication and division
occur before addition and subtraction.
• Operators of the same priority example: multiplication and
division are evaluated from left to right.
• To override the default precedence or to clarify the
statement use parentheses. ()
• Example: without the parentheses operator
SELECT LNAME, FNAME, BOOK, 10 * YR_PUB + 5
FROM AUTHORS;
Rules of Precedence cont.
• Example: with parentheses operator
SELECT LNAME, FNAME, BOOK, 10 * (YR_PUB + 5)
FROM AUTHORS;
Defining NULL values
• Null is a value that is unavailable, unassigned,
unknown, or inapplicable.
• Null is not the same as zero or a blank space.
• If a row lacks a data value for a particular column, that
value is said to be null or to contain a null.
• Example:
SELECT ID, FNAME, TYPE, YR_PUB
FROM AUTHORS;
Null values is Arithmetic Expression
• If the arithmetic expression contains a null
value, the result would also be null.
• For example, if you attempt to perform
multiplication by zero, you get an error.
However, if you multiply a number by null, the
result is a null or unknown.
• Example:
SELECT LNAME, FNAME, BOOK, 10 * YR_PUB * 5
FROM AUTHORS;
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name (There can also be the
optional AS keyword between the column name and the alias.)
• Requires double quotation marks if it contains spaces or special
characters, or if it is case-sensitive
• Example: AS Keyword
SELECT FNAME AS NAME, LNAME AS SURNAME, YR_PUB + 5 AS YEAR
FROM AUTHORS;
A column alias: cont.
• Example: with Double Quotation Mark “”
SELECT FNAME "GIVEN NAME", LNAME "SURNAME", YR_PUB + 5
"YEAR PUBLISHED"
FROM AUTHORS;
FROM AUTHORS;
Concatenation operator:
• Links columns or character strings to other columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character
expression
• Example:
SELECT LNAME||FNAME AS "COMPLETE NAME", BOOK
FROM AUTHORS;
Literal Character Strings
• A literal is either a character, a number, or a date that is included
in the SELECT statement.
• Date and character literal values must be enclosed in single
quotation marks.
• Literal strings of free-format text can be included in the query
result and are treated the same as a column in the SELECT list.
• Each character string is output once for each row returned
• Example:
SELECT LNAME||' , '||FNAME AS "COMPLETE NAME", BOOK
FROM AUTHORS;
Literal Character Strings cont.
• Additional Example: Concatenation two
or more columns:
• Example:
SELECT LNAME||' , '||FNAME|| ' is the author of '
||BOOK as "AUTHORS RECORD"
from AUTHORS
Lesson Summary: