CREATE TABLE table_select ( id1 NUMBER(2,0) , desc1 VARCHAR2(2) );
INSERT INTO table_select (id1, desc1) VALUES (11, 'd1'); INSERT INTO table_select (id1, desc1) VALUES (12, 'd1'); INSERT INTO table_select (id1, desc1) VALUES (13, 'd3'); INSERT INTO table_select (id1, desc1) VALUES (21, 'd1'); COMMIT;The table contains 4 rows with integer IDs and short description codes. This simple structure allows for demonstrating various SELECT operations including filtering, grouping, and ordering.
SELECT
Clause
SELECT [DISTINCT] column_1, column_2, ..., column_nThe SELECT clause specifies which columns or expressions to retrieve from the database. This clause determines the structure and content of the result set. It must be the first clause in any SELECT statement.
SELECT
clause can be:
FROM
clause;SELECT
list can be re-executed for each row returned by the main query, which may impact performance for large result sets.DISTINCT
" operator removes duplicate rows from the result set.
It applies to the entire combination of all columns listed in the SELECT
clause, not individual columns.
Use DISTINCT carefully as it requires Oracle to sort or hash the result set, which can affect performance.SELECT ROWNUM , t1.id1 , NVL(t1.desc1, ' ') AS desc1 , ( SELECT 1 FROM DUAL WHERE MOD(t1.id1, 2) = 0 ) AS "another column" FROM table_select t1This example demonstrates multiple types of SELECT expressions: a pseudocolumn (ROWNUM), table columns (id1, desc1), a function (NVL), and a scalar subquery that returns 1 for even ID values and NULL for odd ones.
-- Select all columns SELECT * FROM table_select; -- Select specific columns SELECT id1, desc1 FROM table_select; -- Select with DISTINCT SELECT DISTINCT desc1 FROM table_select;
FROM
Clause
FROM ( [table|view|subquery] JOIN [table|view|subquery] ON (conditions) )The FROM clause specifies the data sources for the query. This can include tables, views, materialized views, or subqueries. The FROM clause also defines how multiple data sources are joined together using various JOIN operations.
-- Single table SELECT * FROM table_select; -- Table with alias SELECT t.id1, t.desc1 FROM table_select t; -- Using DUAL for testing expressions SELECT SYSDATE FROM DUAL;
WHERE
Clause
WHERE ( conditions )The WHERE clause filters rows before any grouping occurs. It specifies conditions that must be met for a row to be included in the result set.
-- Filter by exact value SELECT * FROM table_select WHERE desc1 = 'd1'; -- Filter by range SELECT * FROM table_select WHERE id1 > 12; -- Multiple conditions SELECT * FROM table_select WHERE id1 >= 12 AND desc1 = 'd1'; -- Pattern matching SELECT * FROM table_select WHERE desc1 LIKE 'd%';
GROUP BY
Clause
GROUP BY ( column_1, column_2, ..., column_n )The GROUP BY clause groups rows that have the same values in specified columns into summary rows. This clause is essential when using aggregate functions to calculate summary statistics for groups of data.
GROUP BY
clause can be any column from the tables listed in the FROM
clause, including columns not selected in the result set.GROUP BY
clause to appear in the SELECT
clause.
However, all non-aggregate columns in the SELECT
clause must appear in the GROUP BY
clause,
except for aggregate functions, constants, and subqueries that do not reference any columns from the FROM
clause
(except for columns already included in the GROUP BY
clause).SELECT t1.desc1 , COUNT(t1.desc1) , 'CONST' , (SELECT 1 FROM DUAL) FROM table_select t1 GROUP BY t1.desc1This example groups rows by the desc1 column, counts occurrences in each group, and includes constant values that are allowed in SELECT with GROUP BY.
HAVING
Clause
HAVING ( conditions )The HAVING clause filters groups created by the GROUP BY clause. Unlike the WHERE clause which filters individual rows, HAVING filters groups based on aggregate values or group characteristics.
-- Filter groups by count SELECT desc1, COUNT(*) FROM table_select GROUP BY desc1 HAVING COUNT(*) > 1; -- Filter groups by aggregate values SELECT desc1, AVG(id1) FROM table_select GROUP BY desc1 HAVING AVG(id1) > 15; -- Combine WHERE and HAVING SELECT desc1, COUNT(*) FROM table_select WHERE id1 > 11 GROUP BY desc1 HAVING COUNT(*) >= 2;
ORDER BY
Clause
ORDER BY column_1 [ASC|DESC], column_2 [ASC|DESC], ..., column_n [ASC|DESC]The ORDER BY clause sorts the final result set based on one or more columns or expressions. This clause is processed last and determines the sequence in which rows are returned to the client application.
-- Sort by single column (ascending by default) SELECT * FROM table_select ORDER BY id1; -- Sort by multiple columns SELECT * FROM table_select ORDER BY desc1, id1 DESC; -- Sort by column position SELECT id1, desc1 FROM table_select ORDER BY 2, 1; -- Sort by expression SELECT id1, desc1 FROM table_select ORDER BY MOD(id1, 10);
FROM
- Tables are identified and joins are performed.WHERE
- Individual rows are filtered based on conditions.GROUP BY
- Remaining rows are grouped by specified columns.HAVING
- Groups are filtered based on aggregate conditions.SELECT
- Columns and expressions are evaluated and result set is formed.ORDER BY
- Final result set is sorted.