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

Lab6 - Data Retrieval Part1

The document outlines the basics of data retrieval using SQL, focusing on the SELECT statement, its syntax, and various clauses such as WHERE, ORDER BY, and the use of operators. It provides examples for selecting distinct values, handling NULL values, and combining conditions with AND, OR, and NOT operators. Additionally, it includes a lab exercise for creating tables and querying data from them.

Uploaded by

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

Lab6 - Data Retrieval Part1

The document outlines the basics of data retrieval using SQL, focusing on the SELECT statement, its syntax, and various clauses such as WHERE, ORDER BY, and the use of operators. It provides examples for selecting distinct values, handling NULL values, and combining conditions with AND, OR, and NOT operators. Additionally, it includes a lab exercise for creating tables and querying data from them.

Uploaded by

bi km
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lab5 – Data Retrieval

Statement & Activity outcome


• Discussions about how to retrieve data from single table with
suitable examples.

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

SELECT Syntax for All columns


If you want to select all the fields available in the table:

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

SELECT Example Without DISTINCT


The following SQL statement selects ALL (including the duplicates)
values from the "Country" column in the "Customers" table:
SELECT DISTINCT Examples
The following SQL statement selects only the DISTINCT values from
the "Country" column in the "Customers" table:
The SQL WHERE Clause
The WHERE clause is used to extract only those records that fulfill a
specified condition.
WHERE Syntax

WHERE Clause Example


The following SQL statement selects all the customers from the country
"Mexico", in the "Customers" table:
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.
Operators in The WHERE Clause
The following operators can be used in the WHERE clause:
Examples of Operators in The WHERE Clause
Here are some examples of applying operators on product table.

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.

How to Test for NULL Values?

• It is not possible to test for NULL values with comparison operators,


such as =, <, or <>.
• We will have to use the IS NULL and IS NOT NULL operators
instead.
IS NULL Syntax
The IS NULL operator is used to test for empty values (NULL values).

IS NOT NULL Syntax


The IS NOT NULL operator is used to test for non-empty values (NOT
NULL values).
Example - IS NULL
The following SQL lists all customers with a NULL value in the
"Address" field:

Example - IS NOT NULL


The following SQL lists all customers with a value in the "Address"
field:
The SQL ORDER BY

• 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.
ORDER BY Syntax

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:

ORDER BY Several Columns Example


The following SQL statement selects all customers from the
"Customers" table, sorted by the "Country" and the "CustomerName"
column. This means that it orders by Country, but if some rows have the
same Country, it orders them by CustomerName:
ORDER BY Several Columns Example 2
The following SQL statement selects all customers from the
"Customers" table, sorted ascending by the "Country" and descending by
the "CustomerName" column:
Lab Exercise
Create the following tables and insert some values.

STUDENT (STDNO, SNAME, DEPTNO#)


DEP (DEPTNO, DNAME)
COURSE (CORSNO, CNAME, DEPTNO#, CMAX)
ENROLMENT (STDNO#, CORSNO#, GRADE, EDATE)

Query a single table


1) Display the whole content of the DEP table
2) Display all columns of the ‘CP’ department
3) Display the whole content of the STUDENT table
4) Display STDNO and SNAME for students in department ‘CP’
7) Display all columns for departments when the Dname starts with letter ‘M’ or ‘C’.
8) Display all columns for courses sorting the result by the CMAX descending.

You might also like