SQL Introduction: Pamantasan NG Cabuyao
SQL Introduction: Pamantasan NG Cabuyao
MODULE
3 SQL INTRODUCTION
OVERVIEW
The purpose of this module introduces the Structured Query Language or SQL to students. SQL (pronounced
"ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database. According
to ANSI (American National Standards Institute), it is the standard language for relational database
management systems. SQL statements are used to perform tasks such as update data on a database, or
retrieve data from a database. Some common relational database management systems that use SQL are:
Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc.
OBJECTIVES
1.1. SQL
In the above section, we learned what we do with the database using SQL. SQL is basically combination of
four different languages, they are –
Copyright Disclaimer
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 1
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021
What is a Query
A Query is a set of instruction given to the database management system, which tells RDBMS what
information you would like to get from the database.
Note: Do not worry about learning the queries now, this is just an introduction. We will learn how to write
queries in detail in the next part.
For e.g. to fetch the employee name from the database table EMPLOYEE, we write the SQL Query like this:
TABLE
A relational database system contains one or more objects called tables. The data or information for the
database are stored in these tables. Tables are uniquely identified by their names and are comprised of
columns and rows. Columns contain the column name, data type, and any other attributes for the column.
Rows contain the records or data for the columns. Here is a sample table called "weather".
city, state, high, and low are the columns. The rows contain the data for this table:
Weather
Flagstaff Arizona 88 69
New
Albuquerque 80 72
Mexico
Copyright Disclaimer
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 2
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021
Since a single server can support many databases, each containing many tables, with each table having a
variety of columns, it’s easy to get lost when you’re working with databases. These commands will help figure
out what’s available:
SHOW DATABASES;
SHOW TABLES IN database;
SHOW COLUMNS IN table;
DESCRIBE table; - shows the columns and their types
Variable Types
SQL supports a very large number of different formats for internal storage of information.
Numeric
Character
Binary
Temporal
DATE
TIME
TIMESTAMP
Copyright Disclaimer
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 3
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021
Suppose we have data measured on the height and weight of children over a range of ages. The first step is
deciding on the appropriate variable types, and creating the table with the CREATE TABLE command.
Selecting Data
The select statement is used to query the database and retrieve selected data that match the criteria that you
specify. Here is the format of a simple select statement:
select "column1"
[,"column2",etc]
from "tablename"
[where "condition"];
[] = optional
The column names that follow the select keyword determine which columns will be returned in the results.
You can select as many column names that you'd like, or you can use a "*" to select all columns.
The table name that follows the keyword from specifies the table that will be queried to retrieve the desired
results. The where clause (optional) specifies which data values or rows will be returned or displayed, based
on the criteria described after the keyword where.
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal to
LIKE *See note below
The LIKE pattern matching operator can also be used in the conditional selection of the where clause. Like is
a very powerful operator that allows you to select only rows that are "like" what you specify.
The percent sign "%" can be used as a wild card to match any possible character that might appear before or
after the characters specified. For example:
Copyright Disclaimer
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 4
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021
This SQL statement will match any first names that start with 'Er'. Strings must be in single quotes. Or you
can specify,
This statement will match any last names that end in a 's'.
This will only select rows where the first name equals 'Eric' exactly.
Copyright Disclaimer
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 5
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021
Comparison Operators
In SQL, the WHERE clause allows you to operate on subsets of a table. The following comparison operators
are
available:
Updating a Table
To change some of the values of columns of a table, you can use the UPDATE command. Changes are provided
as a comma-separated list of column/value pairs. For example, to add one to the weight of an observation in
the kids table where id is 101311 and age is between 9 and 10, we could use:
UPDATE kids SET weight = weight + 1 WHERE id=’101311’ AND age BETWEEN 9 and 10;
Be careful with UPDATE, because if you don’t provide a WHERE clause, all the rows of the table will be
changed.
Create Database
Syntax
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 6
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021
Tip: Make sure you have admin privilege before creating any database. Once a database is created, you can
check it in the list of databases with the following SQL command: SHOW DATABASES;
Note: Be careful before dropping a database. Deleting a database will result in loss of complete information
stored in the database!
Tip: Make sure you have admin privilege before dropping any database. Once a database is dropped, you can
check it in the list of databases with the following SQL command: SHOW DATABASES;
Note: Be careful before dropping a table. Deleting a table will result in loss of complete information stored in
the table!
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER
TABLE statement is also used to add and drop various constraints on an existing table.
Copyright Disclaimer
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 7
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021
Example :
To delete a column in a table, use the following syntax (notice that some database systems don't allow
deleting a column):
The following SQL deletes the "Email" column from the "Customers" table:
Example
To change the data type of a column in a table, use the following syntax:
Name Score
Copyright Disclaimer
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 8
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021
ACTIVITY 1
1) Display the first name and age for everyone that's in the table.
2) Display the first name, last name, and city for everyone that's not from Payson.
3) Display all columns for everyone that is over 40 years old.
4) Display the first and last names for everyone whose last name ends in an "ay".
5) Display all columns for everyone whose first name equals "Mary".
6) Display all columns for everyone whose first name contains "Mary".
IMPORTANT: When selecting a table name, it is important to select a unique name that no one
else will use or guess. Your table names should have an underscore followed by your initials and
the digits of your birth day and month. For example, Tom Smith, who was born on November 2nd,
would name his table myemployees_ts0211 Use this convention for all of the tables you create.
Your tables will remain on a shared database until you drop them, or they will be cleaned up if
they aren't accessed in 4-5 days. If "support" is good, I hope to eventually extend this to at least
one week. When you are finished with your table, it is important to drop your table
ANSWER KEY
Copyright Disclaimer
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 9
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021
2.Display the first name, last name, and city for everyone that's not from Payson.
select first,
last,
city
from empinfo
where city <>
'Payson';
4. Display the first and last names for everyone whose last name ends in an "ay".
II.
Your create statement should resemble:
create table myemployees_ts0211 (firstname varchar(30),
lastname varchar(30), title varchar(30),
age number(2), salary number(8,2) );
Copyright Disclaimer
Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 10