0% found this document useful (0 votes)
83 views

SQL Syntax

This document provides an overview of SQL syntax for common operations like selection, filtering, sorting, aggregation, joins, and more. It covers syntax for SELECT, WHERE, ORDER BY, GROUP BY, HAVING, JOIN, UNION and other clauses. It also discusses functions, comments, stored procedures and other SQL elements.

Uploaded by

Mohammed Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

SQL Syntax

This document provides an overview of SQL syntax for common operations like selection, filtering, sorting, aggregation, joins, and more. It covers syntax for SELECT, WHERE, ORDER BY, GROUP BY, HAVING, JOIN, UNION and other clauses. It also discusses functions, comments, stored procedures and other SQL elements.

Uploaded by

Mohammed Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SQL Syntax

Select Syntax
The SELECT statement is used to select data from a database.

Select Distinct Syntax


The SELECT DISTINCT statement is used to return only distinct (different) values.

Where Clause Syntax


The WHERE clause is used to filter records
And, Or and Not Syntax
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.

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.

SQL Server / MS Access Syntax:

MySQL Syntax:

Oracle 12 Syntax:

Older Oracle Syntax:


MIN and MAX Syntax
The MIN() function returns the smallest value of the selected column.

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:

• The percent sign (%) represents zero, one, or multiple characters


• The underscore sign (_) represents one, single character
WildCard Syntax
A wildcard character is used to substitute one or more characters in a string.

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 IN operator is a shorthand for multiple OR conditions.


Between Syntax
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.

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.

Aliases are often used to make column names more readable.

An alias only exists for the duration of that query.

An alias is created with the AS keyword.

Alias Column Syntax

Alias Table Syntax

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.

Left Join Syntax


The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from the
right side, if there is no match.
Right Join Syntax
The RIGHT JOIN keyword returns all records from the right table (table2), and
the matching records from the left table (table1). The result is 0 records from
the left side, if there is no match.

Full Outer Join Syntax


The FULL OUTER JOIN keyword returns all records when there is a match in left
(table1) or right (table2) table records.

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

UNION ALL 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".

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.

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 and All Syntax


The ANY and ALL operators allow you to perform a comparison between a single
column value and a range of other values.

The SQL ANY Operator

The ANY operator:

• returns a Boolean value as a result


• returns TRUE if ANY of the subquery values meet the condition

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

The ALL operator:

• returns a Boolean value as a result


• returns TRUE if ALL of the subquery values meet the condition
• is used with SELECT, WHERE and HAVING statements

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.

Copy all columns into a new table:

Copy only some columns 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.

Insert Into Select Syntax


The INSERT INTO SELECT statement copies data from one table and inserts it into
another table.
The INSERT INTO SELECT statement requires that the data types in source and
target tables matches.

Note: The existing records in the target table are unaffected.

Copy all columns from one table to another table:

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.

If there is no ELSE part and no conditions are true, it returns NULL.


Stored Procedure Syntax
A stored procedure is a prepared SQL code that you can save, so the code can
be reused over and over again.

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.

Stored Procedure Syntax

Execute a Stored Procedure


NULL Functions Syntax

MySQL
The MySQL IFNULL() function lets you return an alternative value if an
expression is NULL:

or we can use the COALESCE() function, like this:

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.

Single Line Comments

Single line comments start with --.

Any text between -- and the end of the line will be ignored (will not be
executed).

The following example uses a single-line comment as an explanation:

Multi-line Comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be ignored.

The following example uses a multi-line comment as an explanation:


Operators

You might also like