Select

From Oracle FAQ
Jump to: navigation, search

A SELECT is a SQL command used to query (read) data from one or more database objects, typically from a table, view or materialized view.

[edit] Examples

Select all columns (* = all columns) and all rows from the emp table:

SELECT * FROM emp;

Select the salary column for employee number 7788:

SELECT sal
  FROM emp
 WHERE empno = 7788;

Sort rows and return them in order:

SELECT ename, sal FROM emp ORDER BY sal;

Group rows together:

SELECT deptno, 
       COUNT(*) "Employees in department",
       SUM(sal) "Total salaries for department",
       AVG(sal) "Avarage salary for department"
  FROM emp GROUP BY deptno;

[edit] SELECT INTO

PL/SQL and some precompiler languages allow one to select the column values into program variables with the INTO-clause. Typical syntax is:

SELECT ename, sal
  INTO v_name, v_sal
  FROM emp
 WHERE empno = 7788;

Note that this will only work if the query return a single row. If the query returns no rows it will raise a NO_DATA_FOUND exception. If the query returns more than one row, and the variables are not array's, a TOO_MANY_ROWS exception will be raised.

[edit] Also see

Glossary of Terms
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z #