Professional Documents
Culture Documents
2. SELECT Syntax
Here, column1, column2, ... are the field names of the table you want to select data
from. If you want to select all the fields available in the table, use the following
syntax:
Inside a table, a column often contains many duplicate values; and sometimes you
only want to list the different (distinct) values.
6. WHERE Syntax
Note: The WHERE clause is not only used in SELECT statements, it is also used in
UPDATE, DELETE, etc.!
7. The SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one
condition:
• The AND operator displays a record if all the conditions separated by AND are
TRUE.
• The OR operator displays a record if any of the conditions separated by OR is
TRUE.
8. AND Syntax
9. OR Syntax
The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
2. If you are adding values for all the columns of the table, you do not need to specify
the column names in the SQL query. However, make sure the order of the values is in
the same order as the columns in the table. Here, the INSERT INTO syntax would be as
follows:
Note: A NULL value is different from a zero value or a field that contains spaces. A
field with a NULL value is one that has been left blank during record creation!
We will have to use the IS NULL and IS NOT NULL operators instead.
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
18. IS NOT NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Note: Be careful when deleting records in a table! Notice the WHERE clause in the
DELETE statement. The WHERE clause specifies which record(s) should be deleted. If
you omit the WHERE clause, all records in the table will be deleted!
The SELECT TOP clause is useful on large tables with thousands of records. Returning a
large number of records can impact performance.
Note: Not all database systems support the SELECT TOP clause. MySQL supports the
LIMIT clause to select a limited number of records, while Oracle uses FETCH FIRST n
ROWS ONLY and ROWNUM.
SQL Server / MS Access Syntax:
MySQL Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
The MAX() function returns the largest value of the selected column.
SELECT MIN(column_name)
FROM table_name
WHERE condition;
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT SUM(column_name)
FROM table_name
WHERE condition;
There are two wildcards often used in conjunction with the LIKE operator:
Note: MS Access uses an asterisk (*) instead of the percent sign (%), and a question
mark (?) instead of the underscore (_).
The percent sign and the underscore can also be used in combinations!
Tip: You can also combine any number of conditions using AND or OR operators.
Wildcard characters are used with the LIKE operator. The LIKE operator is used in a
WHERE clause to search for a specified pattern in a column.
34. Wildcard Characters in SQL Server
36. IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
The BETWEEN operator is inclusive: begin and end values are included.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
39. SQL Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.
SELECT column_name(s)
FROM table_name AS alias_name;
Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID"
in the "Customers" table. The relationship between the two tables above is the
"CustomerID" column.
Then, we can create the following SQL statement (that contains an INNER JOIN), that
selects records that have matching values in both tables:
43. Example
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Tip: FULL OUTER JOIN and FULL JOIN are the same.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Note: FULL OUTER JOIN can potentially return very large result-sets!
52. SQL Self Join
A self join is a regular join, but the table is joined with itself.
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
• Every SELECT statement within UNION must have the same number of columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
The UNION operator selects only distinct values by default. To allow duplicate values,
use UNION ALL:
Note: The column names in the result-set are usually equal to the column names in
the first SELECT statement.
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(),
SUM(), AVG()) to group the result-set by one or more columns.
58. GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
The EXISTS operator returns TRUE if the subquery returns one or more records.
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
ANY means that the condition will be true if the operation is true for any of the values
in the range.
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);
Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <,
or <=).
ALL means that the condition will be true only if the operation is true for all values in
the range.
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);
Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <,
or <=).
69. The SQL SELECT INTO Statement
The SELECT INTO statement copies data from one table into a new table.
SELECT *
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
The new table will be created with the column-names and types as defined in the old
table. You can create new column names using the AS clause.
The following SQL statement uses the IN clause to copy the table into a new table in
another database:
The following SQL statement copies only a few columns into a new table:
The following SQL statement copies only the German customers into a new table:
Tip: SELECT INTO can also be used to create a new, empty table using the schema of
another. Just add a WHERE clause that causes the query to return no data:
The INSERT INTO SELECT statement requires that the data types in source and target
tables match.
Copy only some columns from one table into another table:
Suppose that the "UnitsOnOrder" column is optional, and may contain NULL values.
In the example above, if any of the "UnitsOnOrder" values are NULL, the result will be
NULL.
77. Solutions
MySQL
The MySQL IFNULL() function lets you return an alternative value if an expression is
NULL:
The SQL Server ISNULL() function lets you return an alternative value when an
expression is NULL:
So if you have an SQL query that you write over and over again, save it as a stored
procedure, and then just call it to execute it.
You can also pass parameters to a stored procedure, so that the stored procedure can
act based on the parameter value(s) that is passed.
EXEC procedure_name;
Note: The examples in this chapter will not work in Firefox and Microsoft
Edge!
Comments are not supported in Microsoft Access databases. Firefox and Microsoft
Edge are using Microsoft Access database in our examples.
82. Single Line Comments
Single line comments start with --.
Any text between -- and the end of the line will be ignored (will not be executed).
83. Example
--Select all:
SELECT * FROM Customers;