Lab6 - Data Retrieval Part1
Lab6 - Data Retrieval Part1
Instructor notes:
After reading the notes given below , answer the questions given in
exercise part and submit in answer sheet with your id and name.
SQL Statements
• SQL statement (select statements) are used for data retrieval.
• SQL statements are not case sensitive.
• SQL statements can be on one or more lines.
• Keywords cannot be abbreviated or split across lines.
• Clauses are usually placed on separate lines.
• Indents are used to enhance readability.
The SQL SELECT Statement
The SELECT statement is used to select data from a database.
SELECT Syntax
Example
Below is a selection from the "Customers" table
SELECT Column Example
The following SQL statement selects the "CustomerName" and "City"
columns from the "Customers" table:
SELECT * Example
The following SQL statement selects all the columns from the
"Customers" table:
The SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct
(different) values.
SELECT DISTINCT Syntax
Example1 - Comparison:
Example2 - Comparison:
Example3 - BETWEEN:
Example4 - IN:
Example5 – LIKE:
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:
o The AND operator displays a record if all the conditions
separated by AND are TRUE.
o 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
OR Syntax
NOT Syntax
AND Example
The following SQL statement selects all fields from "Customers" where
country is "Germany" AND city is "Berlin":
OR Example
The following SQL statement selects all fields from "Customers" where
city is "Berlin" OR "München":
NOT Example
The following SQL statement selects all fields from "Customers" where
country is NOT "Germany":
Combining AND, OR and NOT
You can also combine the AND, OR and NOT operators.
Example1:
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):
Example2:
The following SQL statement selects all fields from "Customers" where
country is NOT "Germany" and NOT "USA":
SQL NULL Values
A field with a NULL value is a field with no value.
ORDER BY Example
The following SQL statement selects all customers from the
"Customers" table, sorted by the "Country" column:
ORDER BY DESC Example
The following SQL statement selects all customers from the
"Customers" table, sorted DESCENDING by the "Country" column: