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

Lab 02

The document discusses the SELECT statement in SQL, which is used to select data from a database table. It explains how to select specific columns or all columns, and how to use DISTINCT, TOP, LIMIT, ORDER BY, and various functions. It also covers aliases and provides examples of SELECT queries.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab 02

The document discusses the SELECT statement in SQL, which is used to select data from a database table. It explains how to select specific columns or all columns, and how to use DISTINCT, TOP, LIMIT, ORDER BY, and various functions. It also covers aliases and provides examples of SELECT queries.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Merit-Quality-Excellence

IBA Institute of Emerging Technologies Khairpur


DATABASE MANAGEMENT SYSTEM
LAB No: 02
Instructor: Marina Gul
Objective of Lab No. 2:
After performing lab2, students will be able to:

o Use Select Statement

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. A SELECT indicates that we are merely reading information, as opposed to
modifying it. What we are selecting is identified by an expression or column list immediately following
the SELECT. Thcmde FROM statement specifies the name of the table or tables from which we are
getting our data.

When you want to select particular fields available in the table, use the following syntax:

SELECT column1, column2, ...

FROM table_name;

SELECT FirstName, LastName

FROM Employees;

Selects data of these two columns from the Employees table

When you want to select all the fields available in the table, use the following syntax:

SELECT *

FROM table_name;

SELECT *

FROM Employees;

Selects all the employees’ records from the database and displays its columns.

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. Syntax:

SELECT DISTINCT column1, column2, ...

FROM table_name;

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

Note: Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to
select a limited number of records while Oracle uses ROWNUM.

SELECT *

FROM table_name

LIMIT number;

The OFF SET value is also most often used together with the LIMIT keyword. The OFF SET value allows us
to specify which row to start from retrieving data.

LIMIT with OFFSET Syntax:

SELECT *

FROM table_name

LIMIT OFFSET, number;

Functions with SELECT Statement

There are some functions that can be used in select statement. Syntax:

SELECT function_name()

FROM table_name;

Functions are:

MIN

MAX

AVG

SUM

COUNT
UPPER

LOWER

LENGTH

etc

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;

ORDER BY Keyword

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.

SELECT column_name(s)

FROM table_name

ORDER BY column1, column2, ... ASC|DESC


Lab Tasks (SELECT Statement)

1. Write a query to display the names (first_name, last_name) using alias name “First
Name", "Last Name".
2. Write a query to get unique department ID from employee table.
3. Write a query to get all employee details from the employee table order by first name,
descending.
4. Write a query to get the employee ID, names (first_name, last_name), salary in
ascending order of salary.
5. Write a query to select first 10 records from a table.
6. Write a query to select 3rd & 4th record of employees table.
7. Write a query to select 2nd last record of employees table.

You might also like