Lab 01: Foundation Statements of SQL
Lab 01: Foundation Statements of SQL
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.
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;
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;
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;
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):
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