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

Lab 11 DB

This document discusses SQL aliases and the SELECT TOP clause. It provides examples of using aliases to give tables or columns temporary names to make SQL queries more readable. It also discusses using the SELECT TOP clause to specify the number or percentage of records returned from a table, giving examples of returning the top 3, 50% of records, and combining TOP with a WHERE clause.

Uploaded by

Malik Mohsin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lab 11 DB

This document discusses SQL aliases and the SELECT TOP clause. It provides examples of using aliases to give tables or columns temporary names to make SQL queries more readable. It also discusses using the SELECT TOP clause to specify the number or percentage of records returned from a table, giving examples of returning the top 3, 50% of records, and combining TOP with a WHERE clause.

Uploaded by

Malik Mohsin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

The Islamia University of Bahawalpur

University College of Engineering & Technology


Department of Cyber Security and Digital Forensics
LAB MANUAL Database Systems 5 TH Semester

LAB EXPERIMENT # 11
SQL ALIAS and Select TOP Clause

Student Name: Muhammad Kamran Roll No: 34

Lab Instructor Signatures: Date:

SQL 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 that query.

• An alias is created with the AS keyword.

select CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country
AS Address FROM Customers;

The following SQL statement creates an alias named "Address" that combine four
columns (Address, PostalCode, City and Country)

Alias for Tables Example

The following SQL statement selects all the orders from the customer with CustomerID=4
(Around the Horn). We use the "Customers" and "Orders" tables, and give them the table
aliases of "c" and "o" respectively (Here we use aliases to make the SQL shorter).
SELECT o.OrderID, o.OrderDate, c.CustomerName FROM Customers AS c, Orders
AS o WHERE c.CustomerName='Around the Horn' AND
c.CustomerID=o.CustomerID;

Output”

SQL TOP, LIMIT, FETCH FIRST or ROWNUM 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.

SELECT TOP number|percent column_name(s) FROM table_name WHERE


condition; SQL TOP, LIMIT and FETCH FIRST Examples

The following SQL statement selects the first three records from the "Customers" table (for
SQL Server/MS Access).

SELECT TOP 3 * FROM Customers;

SELECT * FROM Customers LIMIT 3;

Output:
SELECT * FROM Customers FETCH FIRST 3 ROWS ONLY;

Output:

SQL TOP PERCENT Example

The following SQL statement selects the first 50% of the records from the "Customers" table
(for SQL Server/MS Access):

SELECT TOP 50 PERCENT * FROM Customers;

ADD a WHERE CLAUSE

The following SQL statement selects the first three records from the "Customers" table,
where the country is "Germany" (for SQL Server/MS Access)

SELECT TOP 3 * FROM Customers WHERE Country='Germany';

Output:

You might also like