0% found this document useful (0 votes)
51 views10 pages

Structure Query Language

SQL is a standard language for storing, manipulating, and retrieving data in databases. It allows users to execute queries against a database to retrieve, insert, update, and delete records. Some key SQL commands include SELECT, UPDATE, DELETE, and INSERT. The SELECT statement is used to retrieve data from one or more tables. It returns a result set that can be filtered with clauses like WHERE.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
51 views10 pages

Structure Query Language

SQL is a standard language for storing, manipulating, and retrieving data in databases. It allows users to execute queries against a database to retrieve, insert, update, and delete records. Some key SQL commands include SELECT, UPDATE, DELETE, and INSERT. The SELECT statement is used to retrieve data from one or more tables. It returns a result set that can be filtered with clauses like WHERE.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

Structure Query Language

 standard language for storing, manipulating, and retrieving data in databases.

What Can SQL do?


 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

Some of The Most Important SQL Commands


 SELECT - extracts data from a database
 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database

SQL SELECT Statement
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 Syntax

SELECT column1, column2, ...
FROM table_name; 

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:

SELECT * FROM table_name; 
DEMO TABLE: (note: use this table as reference for the examples to be given below)

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico


Emparedados y Constitución D.F.
helados 2222

3 Antonio Moreno Antonio Mataderos 2312 México 05023 Mexico


Taquería Moreno D.F.

4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

5 Berglunds Christina Berguvsvägen 8 Luleå S-958 22 Sweden


snabbköp Berglund

SELECT Column Example


The following SQL statement selects the "CustomerName" and "City" columns from the "Customers"
table:

SELECT CustomerName, City FROM Customers;

SELECT * Example
The following SQL statement selects all the columns from the "Customers" table:

SELECT * FROM Customers;

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

Inside a table, a column often contains many duplicate values; and sometimes you only
want to list the different (distinct) values.
SELECT DISTINCT Syntax
SELECT DISTINCT column1,  column2, ...
FROM table_name;

The following SQL statement selects only the DISTINCT values from the "Country" column in
the "Customers" table:

SELECT DISTINCT Country FROM Customers;

SQL WHERE Clause


The WHERE clause is used to filter records.

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

WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;

WHERE Clause Example


The following SQL statement selects all the customers from the country "Mexico", in the
"Customers" table:

SELECT * FROM Customers
WHERE Country='Mexico';

Text Fields vs. Numeric Fields


SQL requires single quotes around text values (most database systems will also allow double
quotes).

However, numeric fields should not be enclosed in quotes:

SELECT * FROM Customers
WHERE CustomerID=1;
Operators in The WHERE Clause
The following operators can be used in the WHERE clause:

Operator Description

= Equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

<> Not equal. Note: In some versions of SQL this operator may be written
as !=

BETWEEN Between a certain range

LIKE Search for a pattern

IN To specify multiple possible values for a column


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.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...; 

OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...; 

NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

AND Example
The following SQL statement selects all fields from "Customers" where country is "Germany"
AND city is "Berlin":

SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';

OR Example
The following SQL statement selects all fields from "Customers" where city is "Berlin" OR
"München":

SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
The following SQL statement selects all fields from "Customers" where country is "Germany"
OR "Spain":
SELECT * FROM Customers
WHERE Country='Germany' OR Country='Spain';

NOT Example
The following SQL statement selects all fields from "Customers" where country is NOT
"Germany":

SELECT * FROM Customers
WHERE NOT Country='Germany';

Combining AND, OR and NOT


You can also combine the AND, OR and NOT operators.

The following SQL statement selects all fields from "Customers" where country is "Germany"
AND city must be "Berlin" OR "München" (use parenthesis to form complex expressions):

SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München'); 

The following SQL statement selects all fields from "Customers" where country is NOT
"Germany" and NOT "USA":

SELECT * FROM Customers
WHERE NOT (Country='Germany' AND Country='USA');

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 often used in conjunction with the LIKE operator:

 * - The asterisk represents zero, one, or multiple characters


 ? – The question mark represents a single character

Note: The percent sign and the underscore can also be used in combinations!

LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Here are some examples showing different LIKE operators with '*' and '?' wildcards:

LIKE Operator Description

WHERE CustomerName LIKE Finds any values that start with "a"
'a*'

WHERE CustomerName LIKE Finds any values that end with "a"
'*a'

WHERE CustomerName LIKE Finds any values that have "or" in any position
'*or*'

WHERE CustomerName LIKE Finds any values that have "r" in the second position
'?r*'

WHERE CustomerName LIKE Finds any values that start with "a" and are at least 3
'a??*' characters in length

WHERE ContactName LIKE Finds any values that start with "a" and ends with "o"
'a*o'

SQL LIKE Examples


The following SQL statement selects all customers with a CustomerName starting with "a":

SELECT * FROM Customers
WHERE CustomerName LIKE 'a*';

The following SQL statement selects all customers with a CustomerName ending with "a":

SELECT * FROM Customers
WHERE CustomerName LIKE '*a';

The following SQL statement selects all customers with a CustomerName that have "or" in
any position:
SELECT * FROM Customers
WHERE CustomerName LIKE '*or*';

The following SQL statement selects all customers with a CustomerName that have "r" in the
second position:

SELECT * FROM Customers
WHERE CustomerName LIKE '?r*';

SQL Wildcard Characters


A wildcard character is used to substitute one or more characters in a string.

Wildcard characters are used with the SQL LIKE operator. The LIKE operator is used in a
WHERE clause to search for a specified pattern in a column.

Wildcard Characters in MS Access

Symbol Description Example

* Represents zero or more bl* finds bl, black, blue, and blob
characters

? Represents a single character h?t finds hot, hat, and hit

[] Represents any single character h[oa]t finds hot and hat, but not hit
within the brackets

! Represents any character not in h[!oa]t finds hit, but not hot and hat
the brackets

- Represents a range of characters c[a-b]t finds cat and cbt

# Represents any single numeric 2#5 finds 205, 215, 225, 235, 245, 255,
character 265, 275, 285, and 295
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. 

BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name  BETWEEN value1 AND value2;

Demo Database
Below is a selection from the "Products" table in the Northwind sample database:

ProductID ProductName SupplierID CategoryID Unit Price


1 Chais 1 1 10 boxes x 20 bags 18
2 Chang 1 1 24 - 12 oz bottles 19
3 Aniseed Syrup 1 2 12 - 550 ml bottles 10
4 Chef Anton's Cajun Seasoning 1 2 48 - 6 oz jars 22
5 Chef Anton's Gumbo Mix 1 2 36 boxes 21.35

BETWEEN Example
The following SQL statement selects all products with a price BETWEEN 10 and 20:

Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

NOT BETWEEN Example


To display the products outside the range of the previous example, use NOT BETWEEN:

Example
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
Demo Table
Below is a selection from the "Orders" table in the Northwind sample database:

OrderID CustomerID EmployeeID OrderDate ShipperID


10248 90 5 7/4/1996 3
10249 81 6 7/5/1996 1
10250 34 4 7/8/1996 2
10251 84 3 7/9/1996 1
10252 76 4 7/10/1996 2

The following SQL statement selects all orders with an OrderDate BETWEEN '01-July-1996'
and '31-July-1996':

SELECT * FROM Orders
WHERE OrderDate BETWEEN #01/07/1996# AND #31/07/1996#;

OR:

SELECT * FROM Orders
WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';

You might also like