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

Database Management System 1: SELECT Statement

The document discusses the SQL SELECT statement and its capabilities. It covers selecting specific columns or all columns, writing basic SELECT statements, arithmetic expressions, null values, column aliases, concatenation operators, and literal character strings. Examples are provided for each concept to illustrate proper syntax and usage of the SELECT statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Database Management System 1: SELECT Statement

The document discusses the SQL SELECT statement and its capabilities. It covers selecting specific columns or all columns, writing basic SELECT statements, arithmetic expressions, null values, column aliases, concatenation operators, and literal character strings. Examples are provided for each concept to illustrate proper syntax and usage of the SELECT statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Database Management System 1

SELECT Statement
Lesson Objective

After completing this lesson, the student should


be able to:
• List and enumerate the capabilities of SQL SELECT
statements

• Write and execute a basic SELECT statement


Three application of SELECT
1. Projection
2. Selection
3. Joins
Basic SELECT Statement
• Is used to create a different type of report.
• Syntax:
SELECT (COLUMN_LIST [EXPRESSION])
FROM TBLE_NAME

• 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;

• Example: combination of AS and “” mark


SELECT FNAME AS "GIVEN NAME", LNAME AS "SURNAME", YR_PUB + 5
AS "YEAR PUBLISHED"

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:

In this lesson, you should have learned the following.

• Use the * to call all rows and columns in the table.


• Display specific column by specifying the column lists in
the select list.
• Make use of arithmetic expression
• Rename a column heading
• Make use of concatenation operator as well as character
literal string.

You might also like