0% found this document useful (0 votes)
4 views5 pages

Week 3 - Lab 2 PDF

The document outlines Lab 2 for the CS 630 Database Systems course, focusing on SQL operators and query design using MySQL. It provides objectives for students, tools required, and detailed instructions on using SQL commands such as SELECT, FROM, WHERE, and ORDER BY. Additionally, it includes specific lab tasks to be executed on the Sakila database, along with deliverables for submission.

Uploaded by

as01744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Week 3 - Lab 2 PDF

The document outlines Lab 2 for the CS 630 Database Systems course, focusing on SQL operators and query design using MySQL. It provides objectives for students, tools required, and detailed instructions on using SQL commands such as SELECT, FROM, WHERE, and ORDER BY. Additionally, it includes specific lab tasks to be executed on the Sakila database, along with deliverables for submission.

Uploaded by

as01744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS 630 DATABASE

SYSTEMS
WEEK 3 - LAB 2

ENGR. PROF KASHMALA CHAUDHRY


| MONROE COLLEGE
Lab 2 Assignment
CS 630 DATABASE SYSTEMS

Lab 02: SQL Operators

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

1. Open MySQL Workbench and open the default connection instance.


2. A new query window would open from where you can write and execute queries.

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.

Monroe College Engr. Prof. Kashmala Chaudhry


Lab 2 Assignment
CS 630 DATABASE SYSTEMS

1. SQL Basic Query Structure


The SELECT clause
The most common use of the SQL commands is the selection of data from the tables located in a database.
This can be achieved through SELECT command. We need to SELECT information FROM a table. Hence
we have the most basic SQL query structure comprising of:
• SELECT
• FROM
• WHERE
The syntax for SELECT clause is as follows:

SELECT "column_name(s)" FROM "table_name(s)";

Always specify the name of the database in which a table/relation is present through which data is to be
retrieved.

e.g. Select column_name


From Sakila.table_name

There are three ways we can retrieve data from a table:


• Retrieve one column
• Retrieve multiple columns
• Retrieve all columns (Use *)

The select clause can contain arithmetic expressions involving the operation, +, –, *, and /, and operating
on constants or attributes of records (tuples).

The FROM clause


The FROM clause can allow to select attributes from single table or multiple tables. When multiple tables
are applied, it combines the records from the two or more tables listed and presents every possible
combination of the listed attributes in SELECT clause.
This is not very useful until some filtering condition is applied. This is achieved through using the WHERE
clause.

The WHERE clause


We can use the WHERE clause to filter the result set based on certain conditions. The syntax for
using WHERE in the SELECT statement is as follows:
SELECT "column_name(s)"
FROM "table_name(s)"
WHERE "condition";

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

Monroe College Engr. Prof. Kashmala Chaudhry


Lab 2 Assignment
CS 630 DATABASE SYSTEMS
Operator Description Example
= Equal to Author = 'Alcott'
<> Not equal to (most Dept <> 'Sales'
DBMS also
accept != instead of <>)
> Greater than Hire_Date > '2012-01-31'
< Less than Bonus < 50000.00
>= Greater than or equal Dependants >= 2
<= Less than or equal Rate <= 0.05
BETWEEN Between an inclusive Cost BETWEEN 100.00 AND
range 500.00
LIKE Match a character pattern First_Name LIKE 'Will%'
Table 1: List of Basic Operators available in SQL

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];

2. The Wildcard operators


There are times when we want to match on a string pattern. To do that, we will need to employ the
concept of wildcard. In SQL, there are two wildcards:
• % (percent sign) represents zero, one, or more characters.
• _ (underscore) represents exactly one character.
Wildcards are used with the LIKE keyword in SQL.
Below are some wildcard examples:
• 'A_Z': All string that starts with 'A', another character, and end with 'Z'. For example, 'ABZ' and
'A2Z' would both satisfy the condition, while 'AKKZ' would not (because there are two characters
between A and Z instead of one).
• 'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would both
satisfy the condition.
• '%XYZ': All strings that end with 'XYZ'. For example, 'WXYZ' and 'ZZXYZ' would both satisfy
the condition.
• '%AN%': All strings that contain the pattern 'AN' anywhere. For example, 'LOS ANGELES' and
'SAN FRANCISCO' would both satisfy the condition.
• '_AN%': All strings that contain a character, then 'AN', followed by anything else. For example,
'SAN FRANCISCO' would satisfy the condition, while 'LOS ANGELES' would not satisfy the
condition.

Monroe College Engr. Prof. Kashmala Chaudhry


Lab 2 Assignment
CS 630 DATABASE SYSTEMS

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.

Monroe College Engr. Prof. Kashmala Chaudhry

You might also like