Experiment #3: Retrieving Data Using The SQL SELECT Statement
Experiment #3: Retrieving Data Using The SQL SELECT Statement
Student’s Name:
Semester: Date:
Assessment:
Discussion of results
Participation
Comments:
Objectives:
After completing this experiment, you should be able to do the following:
• List the capabilities of SQL SELECT statements
• Execute a basic SELECT statement
• Returns all rows and columns from a table
• Returns specified columns from a table
• Uses column aliases to display more descriptive column headings
Discussion:
SQL has one basic statement for retrieving information from a database: the SELECT statement.
There are many options and flavors to the SELECT statement in SQL, so we will introduce its
features gradually.
The basic syntax of the SELECT statement is:
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table;
Using the following simple rules and guidelines, you can construct valid statements that are both
easy to read and easy to edit:
• SQL statements are not case sensitive (unless indicated).
• SQL statements can be entered on one or many lines.
• Keywords cannot be split across lines or abbreviated.
• Clauses are usually placed on separate lines for readability and ease of editing.
• Indents should be used to make code more readable.
• Keywords typically are entered in uppercase; all other words, such as table names
and columns, are entered in lowercase.
Column Aliases
When displaying the result of a query, normally uses the name of the selected column as the
column heading. This heading may not be descriptive and, therefore, maybe difficult to
understand. You can change a column heading by using a column alias. Specify the alias after the
column in the SELECT list using a space as a separator. By default, alias headings appear in
uppercase. If the alias contains spaces or special characters (such as # or $), or if it is case
sensitive, enclose the alias in double quotation marks (" ").
Concatenation Operator
You can link columns to other columns, arithmetic expressions, or constant values to create a
character expression by using the concatenation operator (||). Columns on either side of the
operator are combined to make a single output column. If you concatenate a null value with a
character string, the result is a character string.
Duplicate Rows
The SQL displays the results of a query without eliminating duplicate rows. To eliminate duplicate
rows in the result, include the DISTINCT keyword in the SELECT clause immediately after the
SELECT keyword.
Q1: There are four coding errors in the following statement. Can you identify
them?
SELECT empno, ename
salary x 12 ANNUAL SALARY
FROM emp;
Q2: Display the employee name, job, hire date, and employee number for each
employee, with employee number appearing first. Provide an alias STARTDATE
for the HIREDATE column.
Q3: Display all the data from that EMP table. Separate each column output by a
comma. Name the column title THE OUTPUT