Vous êtes sur la page 1sur 8

​ SQL

● SQL stands for Structured Query Language


● SQL lets you access and manipulate databases
● SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International
Organization for Standardization (ISO) in 1987
● SQL can execute queries against a database
● SQL can retrieve data from a database
● SQL can insert records in a database
● SQL can update records in a database
● SQL can delete records from a database
● SQL can create new databases
● SQL can create new tables in a database
● SQL can create stored procedures in a database
● SQL can create views in a database
● SQL can set permissions on tables, procedures, and views

The SQL INSERT SELECT WHERE Statements:


The SELECT statement is used to select data from a database. The data returned is stored in a result table, called
the result-set

SELECT ​* ​FROM ​table_name;


Used to return all values in a table

SELECT ​field_name ​FROM ​table_name;


Used to return field values in a table

SELECT DISTINCT ​field_name ​FROM ​table_name;


is used to return only distinct (different) values

The WHERE clause is used to filter records and to extract only those records that fulfill a specified condition

SELECT ​field_name ​FROM ​table_name


WHERE ​condition;
Example of condition :
WHERE field_name = ‘text’;

SELECT ​field_name ​FROM ​table_name


WHERE NOT ​condition;

The SQL AND, OR and NOT Operators

SELECT ​field_name ​FROM ​table_name


WHERE ​condition1 ​AND ​condition2;

SELECT ​field_name ​FROM ​table_name


WHERE ​condition1 ​OR ​condition2;

The SQL ORDERED BY:


The ORDER BY keyword is used to sort the result-set in ascending or descending order. It sorts the records in
ascending order by default. To sort the records in descending order, use the DESC keyword
SELECT ​field_name ​FROM ​table_name
ORDERED BY​ field_name;
A>>>>>>>Z
SELECT ​field_name​ ​FROM ​table_name
ORDERED BY​ field_name ​DESC​;
Z >>>>>A

The SQL INSERT INTO UPDATE DELETE Statements


The INSERT INTO statement is used to insert new records in a table.

INSERT INTO​ databasename (field1,field2,...)


VALUES ​( ‘ ’ , ……..)

The IS NULL, IS NOT NULL can just be check the null values

SELECT ​field_name ​FROM ​table_name


WHERE ​field_name ​IS NULL​;

The UPDATE statement is used to modify the existing records in a table.

UPDATE ​table_name
SET ​field_name1 = ‘new name’
WHERE ​condition;

The DELETE statement is used to delete existing records in a table.

DELETE FROM ​table_name


WHERE ​condition;

DELETE * FROM ​table_name


WHERE ​condition;
Delete everything

The SQL SELECT TOP Clause

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 on performance.

SELECT TOP 3 * FROM ​table_name


WHERE ​condition;

SELECT 50 PERCENT * FROM ​table_name


WHERE ​condition;

SELECT * FROM ​table_name


LIMIT ​number​;

SELECT * FROM ​table_name


WHERE ROWNUM >=​ number;
The SQL MIN() and MAX() Functions

SELECT MIN (​filedsname​)​ ​ FROM ​table_name


WHERE ​condition;
SELECT MAX (​filedsname​)​ ​ FROM ​table_name
WHERE ​condition;

SELECT MIN (​filedsname) ​AS ​name of this smallest or biggest thing​ FROM ​table_name
WHERE ​condition;

The SQL COUNT(), AVG() and SUM() Functions

The COUNT() function returns the number of rows that matches a specified criteria.
The AVG() function returns the average value of a numeric column.
The SUM() function returns the total sum of a numeric column.

SELECT AVG (​field_name​)​ ​ FROM ​table_name


WHERE ​condition;

The SQL LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards used in conjunction with the LIKE operator:
● % The percent sign represents zero, one, or multiple characters
● _ The underscore represents a single character

WHERE ​field_name ​LIKE ‘​a​%’


Find all values that start with a (accept more than one letter)
WHERE ​field_name ​LIKE ‘​a​’
Find all values that end with a
WHERE ​field_name ​LIKE ‘%​a​%’
Find all values that contain a
WHERE ​field_name ​LIKE ‘-​a​%
’Find all values that a in second location
WHERE ​field_name ​LIKE ‘​a​%​o​’
Find all values that start with a and end with o
WHERE ​field_name ​LIKE ‘​a​-%-%’
Find all values that start with a and contain at least 3 letters
WHERE ​field_name ​NOT LIKE ‘​a​%’
Find all values that don’t start with a

SELECT​ ​column_name(s)
FROM​ ​table_name
WHERE​ ​column_name​ ​IN​ (value1, value2, ...);

The SQL BETWEEN Operator

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.
SELECT​ ​column_name(s)
FROM​ ​table_name
WHERE​ ​column_name​ ​BETWEEN​ ​value1 ​AND ​value2​;

SELECT​ * ​FROM​ table_name


WHERE​ (column_name ​BETWEEN​ value1 ​AND ​value2)
AND​ ​NOT​ olumn_name ​IN​ (values,2,3);

We write date in BETWEEN in the form of #11/!!/2011#


BETWEEN #​date 1​#​ ​AND #​date 2​#
For example date 1 #11/11/2011#

SQL Aliases

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 the query.

Alias Column Syntax


SELECT​ ​column_name​ ​AS​ ​alias_name
FROM​ table_name​;

Alias Table Syntax


SELECT​ ​column_name(s)
FROM​ ​table_name ​AS​ ​alias_name​;

SQL 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:

--Select all:
SELECT​ * ​FROM​ Customers;

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:

/*Select all the columns


of all the records
in the Customers table:*/
SELECT​ * ​FROM​ Customers;
SQL JOIN

A ​JOIN ​clause is used to combine rows from two or more tables, based on a related column between them.
● (INNER) JOIN​​: Returns records that have matching values in both tables
● LEFT (OUTER) JOIN​​: Return all records from the left table, and the matched records from the right
table
● RIGHT (OUTER) JOIN​​: Return all records from the right table, and the matched records from the
left table
● FULL (OUTER) JOIN​​: Return all records when there is a match in either left or right table

column_name(s) take the form table_name.column_name,

(INNER) JOIN
SELECT​ column_name(s)
FROM​ table1
INNER​ ​JOIN​ table2 ​ON​ table1.column_name = table2.column_name;

The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns. If
there are records in the "table1" table that do not have matches in "table2", these orders will not be shown!

LEFT (OUTER) JOIN


SELECT​ column_name(s)
FROM​ table1
LEFT​ ​JOIN​ table2 ​ON​ table1.column_name = table2.column_name;

The LEFT JOIN keyword returns all records from the left table (table1), even if there are no matches in the right
table (table2)

RIGHT (OUTER) JOIN


SELECT​ column_name(s)
FROM​ table1
RIGHT​ ​JOIN​ table2 ​ON​ table1.column_name = table2.column_name;

The RIGHT JOIN keyword returns all records from the right table (table1), even if there are no matches in the
left table (table2).

FULL (OUTER) JOIN


SELECT​ column_name(s)
FROM​ table1
FULL​ ​OUTER​ ​JOIN​ table2 ​ON​ table1.column_name = table2.column_name;
The FULL OUTER JOIN keyword returns all the rows from the left table (table1), and all the rows from the
right table (table2). If there are rows in "table1" that do not have matches in "table2", or if there are rows in
"table2" that do not have matches in "table1", those rows will be listed as well.

SELF JOIN
SELECT​ column_name(s)
FROM​ table1 T1, table1 T2
WHERE​ condition;

A self JOIN is a regular join, but the table is joined with itself.

The SQL UNION Operator

The UNION operator is used to combine the result-set of two or more SELECT statements.
● Each SELECT statement within UNION must have the same number of columns
● The columns must also have similar data types
● The columns in each SELECT statement must also be in the same order

SELECT​ column_name(s) ​FROM​ table1


UNION
SELECT​ column_name(s) ​FROM​ table2;

The SQL GROUP BY 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.

SELECT​ column_name(s)
FROM​ table_name
WHERE​ condition
GROUP​ ​BY​ column_name(s)
ORDER​ ​BY​ column_name(s);
The SQL HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate
functions.

SELECT​ column_name(s)
FROM​ table_name
WHERE​ condition
GROUP​ ​BY​ column_name(s)
HAVING​ condition
ORDER​ ​BY​ column_name(s);

The SQL EXISTS Operator

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.

SELECT​ column_name(s)
FROM​ table_name
WHERE​ ​EXISTS

(​SELECT​ column_name ​FROM​ table_name ​WHERE​ condition);

The SQL ANY and ALL Operators

The ANY and ALL operators are used with a WHERE or HAVING clause.
The ANY operator returns true if any of the subquery values meet the condition.
The ALL operator returns true if all of the subquery values meet the condition.

The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).

SELECT​ column_name(s)
FROM​ table_name
WHERE​ column_name operator ​ANY
(​SELECT​ column_name ​FROM​ table_name ​WHERE​ condition);

SELECT​ column_name(s)
FROM​ table_name
WHERE​ column_name operator ​ALL
(​SELECT​ column_name ​FROM​ table_name ​WHERE​ condition);

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;

SELECT​ column1, column2, column3, ...


INTO​ newtable [​IN​ externaldb]
FROM​ oldtable
WHERE​ condition;

The SQL INSERT INTO SELECT Statement

The INSERT INTO SELECT statement copies data from one table and inserts it into another table.
● INSERT INTO SELECT requires that data types in source and target tables match
● The existing records in the target table are unaffected
INSERT​ ​INTO​ table2
SELECT​ * ​FROM​ table1
WHERE​ condition;

INSERT​ ​INTO​ table2 (column1, column2, column3, ...)


SELECT​ column1, column2, column3, ...
FROM​ table1
WHERE​ condition;

SQL Stored Procedures for SQL Server

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.

CREATE​ ​PROCEDURE​ procedure_name


AS
sql_statement
GO;

EXEC​ procedure_name;

Vous aimerez peut-être aussi