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

SQL Lab 4

The document discusses various SQL commands including DISTINCT, WHERE, AND & OR, ORDER BY, and SELECT TOP. It provides examples of each command using a sample Customers table from the Northwind database. DISTINCT returns only unique/distinct values from a column. WHERE filters records based on conditions. AND/OR allow combining multiple conditions. ORDER BY sorts the results in ascending or descending order. SELECT TOP limits the number of records returned.

Uploaded by

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

SQL Lab 4

The document discusses various SQL commands including DISTINCT, WHERE, AND & OR, ORDER BY, and SELECT TOP. It provides examples of each command using a sample Customers table from the Northwind database. DISTINCT returns only unique/distinct values from a column. WHERE filters records based on conditions. AND/OR allow combining multiple conditions. ORDER BY sorts the results in ascending or descending order. SELECT TOP limits the number of records returned.

Uploaded by

Adnan Asif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB # 4:

SQL DISTINCT, WHERE, AND & OR, ORDER BY, SELECT


TOP COMMANDS

OBJECTIVE (AIM) OF THE EXPERIMENT


To practice and implement SQL Commands (DISTINCT, WHERE, AND & OR, ORDER BY,
SELECT TOP COMMANDS).
EQUIPMENT USED
Sl. Facilities Required Quantity
No.
1 System 1
2 Operating System Windows 7
3 DBMS Sql Server Management Studio
2012

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

The SQL SELECT DISTINCT Statement

In a table, a column may contain many duplicate values; and sometimes you only want to
list the different (distinct) values.

The DISTINCT keyword can be used to return only distinct (different) values.

SQL SELECT DISTINCT Syntax

SELECT DISTINCT column_name,column_name


FROM table_name;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

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 2222 D.F.
helados

3 Antonio Moreno Antonio Moreno Mataderos 2312 México 05023 Mexico


Taquería D.F.
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

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


Berglund
Below is a selection from the "Customers" table:

SELECT DISTINCT Example

The following SQL statement selects only the distinct values from the "City" columns from
the "Customers" table:

Example
SELECT DISTINCT City FROM Customers;

The WHERE clause is used to filter records.

The SQL WHERE Clause

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

SQL WHERE Syntax

SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

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 2222 D.F.
helados

3 Antonio Moreno Antonio Moreno Mataderos 2312 México 05023 Mexico


Taquería D.F.

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

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


Berglund
Below is a selection from the "Customers" table:

WHERE Clause Example

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

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

Example
SELECT * FROM Customers
WHERE CustomerID=1;

Operators in The WHERE Clause

The following operators can be used in the WHERE clause:

Operator Description

= Equal

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

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

BETWEEN Between an inclusive range

LIKE Search for a pattern

IN To specify multiple possible values for a column

The AND & OR operators are used to filter records based on more than one condition.

The SQL AND & OR Operators


The AND operator displays a record if both the first condition AND the second condition are
true.

The OR operator displays a record if either the first condition OR the second condition is
true.

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

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

AND Operator Example

The following SQL statement selects all customers from the country "Germany" AND the city
"Berlin", in the "Customers" table:

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

OR Operator Example

The following SQL statement selects all customers from the city "Berlin" OR "München", in
the "Customers" table:

Example
SELECT * FROM Customers
WHERE City='Berlin'
OR City='München';
Combining AND & OR

You can also combine AND and OR (use parenthesis to form complex expressions).

The following SQL statement selects all customers from the country "Germany" AND the city
must be equal to "Berlin" OR "München", in the "Customers" table:

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

The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in a descending order, you can use the DESC keyword.

SQL ORDER BY Syntax

SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

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
ORDER BY Example

The following SQL statement selects all customers from the "Customers" table, sorted by
the "Country" column:

Example
SELECT * FROM Customers
ORDER BY Country;

ORDER BY DESC Example

The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:

Example
SELECT * FROM Customers
ORDER BY Country DESC;

ORDER BY Several Columns Example

The following SQL statement selects all customers from the "Customers" table, sorted by
the "Country" and the "CustomerName" column:

Example
SELECT * FROM Customers
ORDER BY Country,CustomerName;

The SQL SELECT TOP Clause

The SELECT TOP clause is used to specify the number of records to return.

The SELECT TOP clause can be very useful on large tables with thousands of records.
Returning a large number of records can impact on performance.

Note: Not all database systems support the SELECT TOP clause.

SQL Server / MS Access Syntax

SELECT TOP number|percent column_name(s)


FROM table_name;

SQL SELECT TOP Equivalent in MySQL and Oracle

MySQL Syntax

SELECT column_name(s)
FROM table_name
LIMIT number;
Example

SELECT *
FROM Persons
LIMIT 5;

Oracle Syntax

SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;

Example

SELECT *
FROM Persons
WHERE ROWNUM <=5;

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

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 2222 D.F.
helados

3 Antonio Moreno Antonio Moreno Mataderos 2312 México 05023 Mexico


Taquería D.F.

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

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


Berglund
SQL SELECT TOP Example

The following SQL statement selects the two first records from the "Customers" table:

Example
SELECT TOP 2 * FROM Customers;

SQL SELECT TOP PERCENT Example

The following SQL statement selects the first 50% of the records from the "Customers"
table:

Example
SELECT TOP 50 PERCENT * FROM Customers;

EXPECTED DELIVERABLE

A spool file showing all executions of the above queries.

You might also like