For example,
SELECT name FROM s WHERE city='Rome'This query accesses rows from the table - s. It then filters those rows where the city column contains Rome. Finally, the query retrieves the name column from each filtered row. Using the example s table, this query produces:
| name |
|---|
| Mario |
| sno | name | city |
|---|---|---|
| S1 | Pierre | Paris |
| S2 | John | London |
| S3 | Mario | Rome |
| sno | name | city |
|---|---|---|
| S3 | Mario | Rome |
| name |
|---|
| Mario |
SELECT [ALL|DISTINCT] select-listselect-list is a list of column names separated by commas. The ALL and DISTINCT specifiers are optional. DISTINCT specifies that duplicate rows are discarded. A duplicate row is when each corresponding select-list column has the same value. The default is ALL, which retains duplicate rows.
For example,
SELECT descr, color FROM pThe column names in the select list can be qualified by the appropriate table name:
SELECT p.descr, p.color FROM pA column in the select list can be renamed by following the column name with the new name. For example:
SELECT name supplier, city location FROM sThis produces:
| supplier | location |
|---|---|
| Pierre | Paris |
| John | London |
| Mario | Rome |
A special select list consisting of a single '*' requests all columns in all tables in the FROM clause. For example,
SELECT * FROM sp
| sno | pno | qty |
|---|---|---|
| S1 | P1 | NULL |
| S2 | P1 | 200 |
| S3 | P1 | 1000 |
| S3 | P2 | 200 |
SELECT sp.* FROM spThis produces the same result as the previous example.
An unqualified * cannot be combined with other elements in the select list; it must be stand alone. However, a qualified * can be combined with other elements. For example,
SELECT sp.*, city FROM sp, s WHERE sp.sno=s.sno
| sno | pno | qty | city |
|---|---|---|---|
| S1 | P1 | NULL | Paris |
| S2 | P1 | 200 | London |
| S3 | P1 | 1000 | Rome |
| S3 | P2 | 200 | Rome |
SELECT * FROM sWhen the From List contains multiple tables, commas separate the table names. For example,
SELECT sp.*, city FROM sp, s WHERE sp.sno=s.snoWhen the From List has multiple tables, they must be joined together. See Joining Tables.
SELECT supplier.name FROM s supplierThe new name is known as the correlation (or range) name for the table. Self joins require correlation names.
Following the WHERE keyword is a logical expression, also known as a predicate.
The predicate evaluates to a SQL logical value -- true, false or unknown. The most basic predicate is a comparison:
color = 'Red'This predicate returns:
The = (equals) comparison operator compares two values for equality. Additional comparison operators are:
SELECT * FROM sp WHERE qty >= 200
| sno | pno | qty |
|---|---|---|
| S2 | P1 | 200 |
| S3 | P1 | 1000 |
| S3 | P2 | 200 |
Both operands of a comparison should be the same data type, however automatic conversions are performed between numeric, datetime and interval types. The CAST expression provides explicit type conversions; see Expressions.
The BETWEEN operator implements a range comparison, that is, it tests whether a value is between two other values. BETWEEN comparisons have the following format:
value-1 [NOT] BETWEEN value-2 AND value-3
value-1 >= value-2 AND value-1 <= value-3Or, if NOT is included:
NOT (value-1 >= value-2 AND value-1 <= value-3)For example,
SELECT * FROM sp WHERE qty BETWEEN 50 and 500
| sno | pno | qty |
|---|---|---|
| S2 | P1 | 200 |
| S3 | P2 | 200 |
The IN operator implements comparison to a list of values, that is, it tests whether a value matches any value in a list of values. IN comparisons have the following general format:
value-1 [NOT] IN ( value-2 [, value-3] ... )This comparison tests if value-1 matches value-2 or matches value-3, and so on. It is equivalent to the following logical predicate:
value-1 = value-2 [ OR value-1 = value-3 ] ...or if NOT is included:
NOT (value-1 = value-2 [ OR value-1 = value-3 ] ...)For example,
SELECT name FROM s WHERE city IN ('Rome','Paris')
| name |
|---|
| Pierre |
| Mario |
The LIKE operator implements a pattern match comparison, that is, it matches a string value against a pattern string containing wild-card characters.
The wild-card characters for LIKE are percent -- '%' and underscore -- '_'. Underscore matches any single character. Percent matches zero or more characters.
Examples,
| Match Value | Pattern | Result |
|---|---|---|
| 'abc' | '_b_' | True |
| 'ab' | '_b_' | False |
| 'abc' | '%b%' | True |
| 'ab' | '%b%' | True |
| 'abc' | 'a_' | False |
| 'ab' | 'a_' | True |
| 'abc' | 'a%_' | True |
| 'ab' | 'a%_' | True |
LIKE comparison has the following general format:
value-1 [NOT] LIKE value-2 [ESCAPE value-3]All values must be string (character). This comparison uses value-2 as a pattern to match value-1. The optional ESCAPE sub-clause specifies an escape character for the pattern, allowing the pattern to use '%' and '_' (and the escape character) for matching. The ESCAPE value must be a single character string. In the pattern, the ESCAPE character precedes any character to be escaped.
For example, to match a string ending with '%', use:
x LIKE '%/%' ESCAPE '/'A more contrived example that escapes the escape character:
y LIKE '/%//%' ESCAPE '/'... matches any string beginning with '%/'.
The optional NOT reverses the result so that:
z NOT LIKE 'abc%'is equivalent to:
NOT z LIKE 'abc%'
A database null in a table column has a special meaning -- the value of the column is not currently known (missing), however its value may be known at a later time. A database null may represent any value in the future, but the value is not available at this time. Since two null columns may eventually be assigned different values, one null can't be compared to another in the conventional way. The following syntax is illegal in SQL:
WHERE qty = NULLA special comparison operator -- IS NULL, tests a column for null. It has the following general format:
value-1 IS [NOT] NULLThis comparison returns true if value-1 contains a null and false otherwise. The optional NOT reverses the result:
value-1 IS NOT NULLis equivalent to:
NOT value-1 IS NULLFor example,
SELECT * FROM sp WHERE qty IS NULL
| sno | pno | qty |
|---|---|---|
| S1 | P1 | NULL |
The AND operator combines two logical operands. The operands are comparisons or logical expressions. It has the following general format:
predicate-1 AND predicate-2
Truth tables for AND:
|
|
For example,
SELECT * FROM sp WHERE sno='S3' AND qty < 500
| sno | pno | qty |
|---|---|---|
| S3 | P2 | 200 |
The OR operator combines two logical operands. The operands are comparisons or logical expressions. It has the following general format:
predicate-1 OR predicate-2OR returns:
Truth tables for OR:
|
|
For example,
SELECT * FROM s WHERE sno='S3' OR city = 'London'
| sno | name | city |
|---|---|---|
| S2 | John | London |
| S3 | Mario | Rome |
AND has a higher precedence than OR, so the following expression:
a OR b AND cis equivalent to:
a OR (b AND c)
The NOT operator inverts the result of a comparison expression or a logical expression. It has the following general format:
NOT predicate-1
Truth tables for NOT:
|
|
Example query:
SELECT * FROM sp WHERE NOT sno = 'S3'
| sno | pno | qty |
|---|---|---|
| S1 | P1 | NULL |
| S2 | P1 | 200 |
| SQL-Data Statements | SQL Tutorial Main Page |