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

Lab 01: Foundation Statements of SQL

This document outlines the objectives and tasks for Lab 01 of the CSC-252 Database Management Systems course. It covers installing MySQL, connecting to databases from the command line, SQL statements like SELECT, DISTINCT, ORDER BY, and functions. The lab exercises students on using these statements to query data from sample HR tables, with tasks like selecting unique records, aggregating data, sorting results, and limiting the number of records returned.

Uploaded by

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

Lab 01: Foundation Statements of SQL

This document outlines the objectives and tasks for Lab 01 of the CSC-252 Database Management Systems course. It covers installing MySQL, connecting to databases from the command line, SQL statements like SELECT, DISTINCT, ORDER BY, and functions. The lab exercises students on using these statements to query data from sample HR tables, with tasks like selecting unique records, aggregating data, sorting results, and limiting the number of records returned.

Uploaded by

Sonu Sachdev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CSC-251: Database Management System

Semester IV(CS, SE) Section(A, B) (Spring 2021)


Course Instructor(s): Saif Hassan
Lab designed by: Khalid Hussain

Lab 01: Foundation statements of SQL


Objective(s):
1. MYSQL Installation
2. Connect to MYSQL Database from Command Line
3. What is SQL?
4. USE Statement
5. SELECT Statement.
6. DISTINCT Statement.
7. SELECT Top Clause.
8. Functions with SELECT Statement.
9. Aliases
10. ORDER BY Keyword

1: MYSQL Installation
Step 01:
Download MYSQL
Step 02:

1
Department of Computer Science
CSC-252: Database Management System Lab 01: Foundation statements of SQL
Step 03:

Step 04:

2
Department of Computer Science
CSC-252: Database Management System Lab 01: Foundation statements of SQL
Step 05:

Step 06:
Run downloaded file and follow steps in wizard.

2: Connect to MYSQL Database from Command Line

3
Department of Computer Science
CSC-252: Database Management System Lab 01: Foundation statements of SQL
3: What is SQL?
SQL is a standard language for accessing and manipulating databases. SQL stands for
Structured Query Language, it lets you access and manipulate databases.
SQL can do…
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views

4: USE Statement
To select a particular database to work with you issue the USE statement as follows:
USE database_name;
In this statement, following the USE keyword is the name of the database that you want to
select.

4
Department of Computer Science
CSC-252: Database Management System Lab 01: Foundation statements of SQL
5: 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. The 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;
Example:
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;
Example:
SELECT *
FROM Employees;
Selects all the employees records from the database and displays its columns.

6: 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;

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

MYSQL Syntax:
5
Department of Computer Science
CSC-252: Database Management System Lab 01: Foundation statements of SQL
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;

Visit link for Oracle & SQL Server Syntax

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

9: 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;

6
Department of Computer Science
CSC-252: Database Management System Lab 01: Foundation statements of SQL
10: 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.
Syntax:
SELECT column_name(s)
FROM table_name
ORDER BY column1, column2, ... ASC|DESC

Lab Task(s):

Exercise 1 (MYSQL Installation & Use of Command Line)

1. Download and install MYSQL in your computer.


2. Import provided “hr.sql” database by using command line.
3. Connect imported database from command line.

Exercise 2 (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 get the total salaries payable to employees.
6. Write a query to get the maximum and minimum salary from employees table.
7. Write a query to get the average salary and number of employees in the employees
table.
8. Write a query to get the number of jobs available in the employees table.
9. Write a query get all first name from employees table in upper case.
10. Write a query to select first 10 records from a table.
11. Write a query to select 3rd & 4th record of employees table.
12. Write a query to select 2nd last record of employees table.

END

7
Department of Computer Science
CSC-252: Database Management System Lab 01: Foundation statements of SQL

You might also like