IT130-44-Week 6 Lecture Notes
IT130-44-Week 6 Lecture Notes
Anthony Rodriguez
Objectives:
• Examine Structured Query Language (SQL)
• Create tables and fields with SQL
• Select data using criteria in SQL
• Select data using AND, OR, BETWEEN, and NOT operators
• Create computed fields in SQL
• Use wildcards and the LIKE and IN operators
• Apply built-in SQL aggregate functions
• Group records in SQL
• Use subqueries in SQL
• Join tables using SQL
• Perform union operations in SQL
• Use SQL to update, insert, and delete records
• Prepare for a database career by acquiring SQL skills
SQL is the go-to language for web-based computing and is widely used in various applications such as
human resources management, supply chain management, customer relationship management, and
enterprise resource planning. Despite being a technical language, it is not limited to information
technology professionals and is often a required skill for employees in organizations that use corporate
databases. ISO and ANSI both acknowledge SQL as a standard language, though individual relational
DBMSs may only support a limited subset of SQL commands.
SQL serves as a data definition language, data manipulation language, and data control language. The
CREATE TABLE command is a data definition command, while the SELECT command is a data
manipulation command. Data control commands are covered in Chapter 4. SQL is a non-procedural
language, meaning it specifies what needs to be done, not how to do it.
For more information on the popular database MySQL, visit the MySQL website at www.mysql.com/. It
is the backend database for many high-traffic websites, including Facebook and Google.
Table Creation
You use the SQL command “CREATE TABLE <tablename>” to create a new table. By using this command,
we describe the layout (fields and data types) of the table. There are common restrictions on table and
column names:
An example of the “CREATE TABLE” command can be found on page 90 of the text.
Simple Retrieval
The basic form of an SQL command to retrieve data is
The result of this command will display all student names and student IDs from the students table.
You can list all fields and all records in a table by using the asterisk (*) wildcard symbol:
If you want to add a condition to your query, you will use the optional WHERE clause. For
example:
= Equal to
Compound conditions
You can expand the scope of your results by combining one or more simple conditions using the “AND”
or “OR” operators. Note that if you use the “AND” operator, all conditions must be true; if you use the
OR operator, only one of the conditions must be true. For example:
SELECT * FROM students WHERE major = ‘Computer Science’ AND gpa >= 3.5;
Only records where the major is Computer Science and the GPA is greater than or equal to 3.5 will be
selected.
SELECT * FROM students WHERE major = ‘Computer Science’ OR major = ‘Information Technology’;
Records where the major is either Computer Science or Information Technology will be selected.
You can use also other compound operators such as NOT (to illustrate negation) and BETWEEN (to
illustrate a range of values):
This command will display the tuition fee and payment fields from the finance table and create a new
calculated/computed field showing the results of tuitionfee – payments. That new field will have the
field name ‘Balance’ in the query results.
Special Operators
There are special operators in SQL that allow you to query inexact matches. For example, you might only
know certain letters that comprise a name. In such cases, you use the LIKE operator with a wildcard. For
example, to look for students whose first name begin with ‘F’:
Computer Engineering’);
Sorting
You sort your query results by using the “ORDER BY” clause. The field on which to sort data is called a
sort key. When you need to sort data on two (or more) fields, the more important sort key is called the
major sort key (or primary sort key). You can also specify the sort order by using DESC (for descending)
or ASC (for ascending, which is default in most systems). For example:
SELECT * FROM students ORDER BY gpa; (that is, show all students from highest to lowest GPA)
(that is, show all students from highest to lowest GPA; if they have same GPA, sort by lastname)
Built-in functions
SQL has built-in functions (also known as aggregate functions) to calculate the number of entities
(COUNT), the sum (SUM) or average (AVG) of all the entities in a given column, and the largest (MAX) or
smallest (MIN) values in a given column.
→→ average age
Subqueries
In some cases, it is useful to obtain the results in two stages. You can do so by placing one query inside
another, called a subquery. It is important to note that the subquery is evaluated first, then the other
query.
SELECT * FROM students WHERE major = ‘Computer Science’ AND location IN (SELECT city FROM
studentMap WHERE state = ‘New York’);
Grouping
Recall that grouping creates groups of records that share some common characteristic. In SQL, we
accomplish this by using the GROUP BY clause. If you want to extend or further qualify the results of
queries with GROUP BY clause, you can use the HAVING clause (not the WHERE clause).
→ (lists all the majors and the number of students enrolled in that major)
SELECT COUNT(*), majors FROM students GROUP BY majors HAVING COUNT(*) > 5;
Joining tables
The following steps are required to join tables:
Union
The union of two tables results in a table which contains all rows form the first table, the second table,
or both. Tables involved in a union must be union compatible, that is, they must have the same number
of fields with corresponding data types; they must have the same structure.
An example of the “UNION” command can be found on page 113 of the text.
Update The UPDATE command in SQL allows changes to be made to existing data in a table. For
example:
→ (changes the student’s major to Business for the student whose ID is 135-97 in the students table)
Key Terms
After reading Module 3, these are the key terms you should be familiar with.
• & operator
• aggregate function
• alias
• American National Standards Institute (ANSI)
• American Standard Code for Information Interchange (ASCII)
• AND operator
• AUTOINCREMENT
• AutoNumber
• BETWEEN operator
• Boolean
• built-in functions
• BYTE
• calculated field
• CHAR
• compound condition
• computed field
• concatenation
• CREATE TABLE
• data-definition language (DDL)
• DELETE command
• FROM clause
• GROUP BY clause
• HAVING clause
• INSERT command
• INSERT INTO clause
• International Organization for Standardization (IOS)
• keyword
• logical view
• monospaced
• NOT operator
• null
• OR operator
• ORDER BY clause
• qualified field names
• reserved word
• SELECT clause
• SELECT-FROM-WHERE
• simple condition
• SQL (Structured Query Language)
• subquery
• syntax
• VALUES
• view
• WHERE clause
• wildcard character