SQL Syntax
SQL Syntax
Select Syntax
The SELECT statement is used to select data from a database.
The AND and OR operators are used to filter records based on more than one
condition:
Order By Syntax
The ORDER BY keyword is used to sort the result-set in ascending or descending
order.
The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.
Insert Into Syntax
The INSERT INTO statement is used to insert new records in a table.
Update Syntax
The UPDATE statement is used to modify the existing records in a table.
Delete Syntax
The DELETE statement is used to delete existing records in a table.
Select Top Syntax
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of records.
Returning a large number of records can impact performance.
MySQL Syntax:
Oracle 12 Syntax:
The MAX() function returns the largest value of the selected column.
Count Syntax
The COUNT() function returns the number of rows that matches a specified
criterion.
Average Syntax
The AVG() function returns the average value of a numeric column.
Sum Syntax
The SUM() function returns the total sum of a numeric column.
Like Syntax
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
There are two wildcards often used in conjunction with the LIKE operator:
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.
IN Syntax
The IN operator allows you to specify multiple values in a WHERE clause.
The BETWEEN operator is inclusive: begin and end values are included.
Alias Syntax
SQL aliases are used to give a table, or a column in a table, a temporary name.
Join Syntax
A JOIN clause is used to combine rows from two or more tables, based on a
related column between them.
Inner Join Syntax
The INNER JOIN keyword selects records that have matching values in both
tables.
Tip: FULL OUTER JOIN and FULL JOIN are the same.
A self-join is a regular join, but the table is joined with itself.
Union Syntax
The UNION operator is used to combine the result-set of two or
more SELECT statements.
• 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
UNION Syntax
Group By Syntax
The GROUP BY statement groups rows that have the same values into summary
rows, like "find the number of customers in each country".
Having Syntax
The HAVING clause was added to SQL because the WHERE keyword cannot be
used with aggregate functions.
Exists Syntax
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
ANY means that the condition will be true if the operation is true for any of the
values in the range.
The SQL ALL Operator
ALL means that the condition will be true only if the operation is true for all
values in the range.
Select Into Syntax
The SELECT INTO statement copies data from one table into a new table.
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.
Copy only some columns from one table into another table:
Case Syntax
The CASE statement goes through conditions and returns a value when the first
condition is met (like an if-then-else statement). So, once a condition is true, it
will stop reading and return the result. If no conditions are true, it returns the
value in the ELSE clause.
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.
MySQL
The MySQL IFNULL() function lets you return an alternative value if an
expression is NULL:
SQL Server
The SQL Server ISNULL() function lets you return an alternative value when an
expression is NULL:
MS Access
The MS Access IsNull() function returns TRUE (-1) if the expression is a null
value, otherwise FALSE (0):
Comments
Comments are used to explain sections of SQL statements, or to prevent execution
of SQL statements.
Any text between -- and the end of the line will be ignored (will not be
executed).
Multi-line Comments