You can retrieve data from rows stored one or more database tables or views with a query using the SQL SELECT statement. The SELECT statement retrieves the all of or part of the column data from rows depending on the conditions that you specify in WHERE clauses. The group of columns that are selected from a table is referred to as the SELECT list.
With the SQL SELECT statement, you can query and display data of tables in a database.
Example: Using SELECT to Retrieve Data shows how to use SELECT to retrieve data from the employees table. In this example, the data for all columns in a row (record) of the employees table is retrieved with the use of the wildcard (*) notation. Note the use of comments to document the SQL statements. The comments (or remarks) in this example begin with --.
Using SELECT to Retrieve Data
-- the following uses the wildcard * to retrieve all the columns of data in -- all rows of the employees table SELECT * FROM employees;
Example: Using SELECT to Retrieve Data From Specific Columns shows how to use SELECT to retrieve the data for specific columns of the employees table. In this example, you explicitly enter the column names in the SELECT statement.
Using SELECT to Retrieve Data From Specific Columns
-- the following retrieves the data in columns employee_id, last_name, first_name SELECT employee_id, last_name, first_name FROM employees;
|
See Also:
|
Many SQL statements, functions, expressions, and conditions require you to specify character literal values. You can specify character literals with the following notations:
Character literals with the 'text' notation, as in the literals 'users01.dbf' and 'Muthu''s computer'.
National character literals with the N'text' or n'text' notation, where N or n specifies the literal using the national character set. For example, N'résumé' is a National character literal.
For information on unicode literals, see Unicode String Literals.
By default you must quote character literals in single-quotes, as in 'Hello'. This technique can sometimes be inconvenient if the text itself contains single quotes. In such cases, you can also use the Q-quote mechanism, which enables you to specify q or Q followed by a single quote and then another character to be used as the quote delimiter. For example, the literal q'#it's the "final" deadline#' uses the pound sign (#) as a quote delimiter for the string it's the "final" deadline.
The Q-quote delimiter can be any single- or multibyte character except space, tab, and return. If the opening quote delimiter is a [, {, <, or ( character, then the closing quote delimiter must be the corresponding ], }, >, or ) character. In all other cases, the opening and closing delimiter must be the identical character.
The following character literals use the alternative quoting mechanism:
q'(name LIKE '%DBMS_%%')'q'<'Data,' he said, 'Make it so.'>'q'"name like '['"'nq'ïŸ1234ï'
|
See Also:
|
When displaying the result of a query, SQL normally uses the name of the selected column as the column heading. You can change a column heading by using a column alias to make the heading more descriptive and easier to understand.
You can specify the alias after the column name in the SELECT list using a space as a separator. If the alias contains spaces or special characters (such as # or $), or if it is case-sensitive, enclose the alias in double quotation marks (" ").
Example: Using a Column Alias shows the use of a column alias to provide more description for each heading of the columns selected in a query.
Using a Column Alias
-- the following retrieves the data in columns employee_id, last_name, first_name -- and provides column aliases for more descriptive headings of the columns SELECT employee_id "Employee Id number", last_name "Employee last name", first_name "Employee first name" FROM employees;
The WHERE clause uses comparison operators to identify specific rows in a table. When used with the SELECT statement, you can selectively retrieve rows from a table rather than retrieving all rows of a table.
Comparison operators include those listed in Table: Comparison Operators.
Comparison Operators
| Operator | Definition |
|---|---|
|
=, !=, <> |
test for equal, not equal, not equal |
|
>, >=, <, <= |
test greater than, greater than or equal to, less than, less than or equal to |
|
BETWEEN ... AND ... |
check for range between and including two values |
|
LIKE |
search for a match in string, using the wildcard symbols % (zero or multiple characters) or _ (one character) |
|
IN ( ) |
test for a match in a specified list of values |
|
IS NULL, IS NOT NULL |
check whether is null (no value), is not null |
Example: Using SELECT With a WHERE Clause shows how to use SELECT with a WHERE clause and several comparison operators to retrieve specific rows of data from the employees table.
Using SELECT With a WHERE Clause
-- the following retrieves data where the manager_id equals 122
SELECT * FROM employees WHERE manager_id = 122;
-- this retrieves data where the manager_id equals 122 and job_id is ST_CLERK
SELECT * FROM employees WHERE manager_id = 122 AND job_id = 'ST_CLERK';
-- this retrieves employees with managers with Ids between 122 and 125 inclusive
SELECT * FROM employees WHERE manager_id BETWEEN 122 AND 125;
-- this uses the wildcard % to retrieve employee data
-- where the last name contains mar somewhere in the name
SELECT employee_id, last_name FROM employees WHERE last_name LIKE '%mar%';
-- this retrieves employees where the last name starts with Mar
SELECT employee_id, last_name FROM employees WHERE last_name LIKE 'Mar%';
-- this retrieves employees where the commission percentage is not null
SELECT employee_id, last_name FROM employees WHERE commission_pct IS NOT NULL;
-- the following retrieves data where the employee_id equals 125, 130, or 135
SELECT employee_id, last_name, first_name FROM employees
WHERE employee_id IN (125, 130, 135);
|
See Also:
|
You can use SELECT with the ORDER BY clause to retrieve and display rows from a table ordered (sorted) by a specified column in the table. The specified column in the ORDER BY clause does not have to be in the select-list of columns that you want to display.
You can specify the sort order ASC for ascending or DESC for descending. The default sort order is ascending, which means:
Numeric values are displayed with the lowest values first, such as 1 to 999.
Character values are displayed in alphabetical order, such as A first and Z last.
Date values are displayed with the earliest value first, such as 01-JUN-93 before 01-JUN-95.
Null (empty) values are displayed last for ascending sequences and first for descending sequences.
Example: Using SELECT With ORDER BY shows how to use SELECT with the ORDER BY clause to retrieve and display rows from the employees table ordered (sorted) by specified columns.
Using SELECT With ORDER BY
-- the following retrieves rows with manager_id = 122 ordered by employee_id
-- the order is the default ascending order, lowest employee_id displays first
SELECT * FROM employees WHERE manager_id = 122 ORDER BY employee_id;
-- the following retrieves rows ordered by manager_id
-- the order is specified as descending, highest manager_id displays first
SELECT employee_id, last_name, first_name, manager_id FROM employees
ORDER BY manager_id DESC;
See Example: Using Aggregate Functions for the use of ORDER BY with the GROUP BY clause.
|
See Also:
|
You can use SELECT to display data from the multiple tables. This process is referred to as joining the tables. In a join, multiple tables share a similar column.
When you retrieve data from multiple tables, you can explicitly identify which table a column belongs to. This is important when tables contain columns with the same name. You can use the complete table name to explicitly identify a column, such as employees.employee_id, or a table alias. Note the use of the table aliases (d, e, and l) to explicitly identify the columns by table in the SQL statement. The alias is defined in the FROM clause of the SQL statement. A table alias is used to simply and reduce the size of the SQL code.
Example: Selecting Data From Multiple Tables With the ANSI Join Syntax is an example of querying data from joined tables using ANSI syntax. The first SELECT joins two tables and the second SELECT joins three tables.
Selecting Data From Multiple Tables With the ANSI Join Syntax
-- the following SELECT statements retrieve data from two tables -- that have a corresponding column (department_id) -- this join uses ANSI syntax, note the use of JOIN and ON SELECT e.employee_id, e.last_name, e.first_name, e.department_id, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id; -- the following SELECT retrieves data from three tables -- two tables have the corresponding column department_id and -- two tables have the corresponding column location_id SELECT e.employee_id, e.last_name, e.first_name, e.department_id, d.department_name, d.location_id, l.country_id FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN locations l ON d.location_id = l.location_id;
In Example: Using SELECT to Display Data From Multiple Tables the joins use the Oracle-proprietary syntax. There is no performance difference between the ANSI and Oracle syntax.
Using SELECT to Display Data From Multiple Tables
-- the following SELECT statements retrieve data from two tables -- that have a corresponding column (department_id) -- note that the employees table has been aliased to e and departments to d SELECT e.employee_id, e.last_name, e.first_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id; -- the following SELECT retrieves data from three tables -- two tables have the corresponding column department_id and -- two tables have the corresponding column location_id SELECT e.employee_id, e.department_id, d.department_name, d.location_id, l.country_id FROM employees e, departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id;
|
See Also:
|