Week 3 - Lab 2 PDF
Week 3 - Lab 2 PDF
SYSTEMS
WEEK 3 - LAB 2
Introduction
Structured Query Language (SQL) was developed at IBM San Jose Research Laboratory as a part
of System R project. It is a declarative query language for querying a relational database. It also
includes features for defining the structure of the data, for inserting and modifying data in the
database, and for specifying security constraints. It is relational complete (it supports all six core
relational algebra operations). SQL commands can be classified into three groups DDL, DML &
DCL.
Objectives
After performing this lab students should be able to:
1. Design SQL queries to retrieve data using SELECT clause and various associated operators.
2. Design SQL queries with FROM & WHERE clause.
3. Execute SQL queries over MySQL using MySQL Workbench.
Tools/Software Requirement
• MySQL Community Server
• MySQL Workbench
• Sakila Database
Description
3. You can save the query file and can also add comments using # symbol.
4. On executing queries, results are displayed in the lower part of the screen.
5. Error or success messages are displayed in action output pane at the bottom.
6. Try running few SQL queries modeled during the lectures to get it going.
7. Continue playing with the Workbench and SQL queries till you are comfortable with the querying
mechanism and have learnt the shortcuts to execute queries.
Always specify the name of the database in which a table/relation is present through which data is to be
retrieved.
The select clause can contain arithmetic expressions involving the operation, +, –, *, and /, and operating
on constants or attributes of records (tuples).
"Condition" can include a single comparison clause (called simple condition) or multiple comparison
clauses combined together using AND or OR operators (compound condition). Conditions can include
other operators like IN, BETWEEN, DISTINCT etc. shown in Table 1:
1. Ordering data
The order of rows returned in a query result is undefined. The ORDER BY clause can be used to sort the
rows. This clause comes last in the SELECT statement. ASC at the end of the ORDER BY clause specifies
ascending order where as DESC specifies descending order. ASC is the default order. The syntax for
an ORDER BY statement is as follows:
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC];
Lab Task
Using Sakila Database
Formulate SQL queries for the following needs and execute them on the Sakila database.
1. Write a query to Select column “actor_id” from the table “actor” which already exists in
the “Sakila” database where the “actor_id=58”. (Observe the output when you execute
the queries.)
2. Write a query to retrieve the names of movies starting with P.
3. Write a query to retrieve movies that were released in the year 2006.
4. What password did the DBA assign to the user „MIKE‟?
5. Write a query to retrieve data of all actors whose names are not ending in T.
6. Find and display the Income (payments) generated during August 2005. Sort them in
descending order.
7. Select the names of actors whose IDs are between 50 and 150, or those whose last name
starts with A.
8. Display the actor names that start with A and end on the letter A.
9. Write an SQL query to display list of languages which neither is English nor French.
10. Find all actors whose last name contain the letters “GEN”
Deliverables
Submit a PDF document including the SQL queries to answer the above-mentioned information
as well as a screenshot of their outcome when executed using the MYSQL Workbench.