0% found this document useful (0 votes)
82 views7 pages

IT130-44-Week 6 Lecture Notes

This document provides an overview of Structured Query Language (SQL) concepts and commands covered in Module 3, including: - Creating tables and defining fields using SQL commands like CREATE TABLE - Selecting data using criteria and operators like AND, OR, BETWEEN, and NOT in SQL queries - Creating computed fields and using special operators like wildcards, LIKE, and IN - Grouping records, performing aggregate functions, and using subqueries in SQL - Joining tables and performing union operations using SQL commands - Updating, inserting, and deleting records from tables using SQL commands

Uploaded by

Mhea
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)
82 views7 pages

IT130-44-Week 6 Lecture Notes

This document provides an overview of Structured Query Language (SQL) concepts and commands covered in Module 3, including: - Creating tables and defining fields using SQL commands like CREATE TABLE - Selecting data using criteria and operators like AND, OR, BETWEEN, and NOT in SQL queries - Creating computed fields and using special operators like wildcards, LIKE, and IN - Grouping records, performing aggregate functions, and using subqueries in SQL - Joining tables and performing union operations using SQL commands - Updating, inserting, and deleting records from tables using SQL commands

Uploaded by

Mhea
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/ 7

IT130-44/Database

Anthony Rodriguez

Module 3 - LECTURE NOTES

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

Structured Query Language (SQL)


The current chapter delves into Structured Query Language (SQL), the foundation of all modern DBMSs.
SQL enables users to access and query databases through commands, including creating tables,
retrieving data from tables, and updating them. The commands used for data retrieval are commonly
referred to as queries.

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:

• Names cannot exceed 18 characters


• Names must start with a letter
• Names can contain only letters, numbers, and underscores (_)
• Names cannot contain spaces

Here are some of the common data types in SQL:

INTEGER, SMALLINT, DECIMAL(p,q)*, CHAR(n), DATE


(*Access does not support DECIMAL, CURRENCY or NUMBER may be used.)

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

SELECT (column names)-FROM (table names)-WHERE (condition);


Note that the WHERE clause is optional. For example:
SELECT studentName, studentID FROM students;

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:

SELECT * FROM students;

If you want to add a condition to your query, you will use the optional WHERE clause. For
example:

SELECT * FROM students WHERE major = ‘Computer Science’;

Note: The comparison operators are:

= Equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

<> Not 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):

SELECT * FROM students WHERE NOT major = ‘Computer Science’;

SELECT * FROM students WHERE age BETWEEN 20 and 25;


Computed Fields
You can include fields in queries that are not in the database but whose values you can compute from
existing database fields. A field whose values you can derive is called a computed or calculated field. You
use the “AS” class to name a computed field. For example:

SELECT tuitionFee, payments, tuitionFee - payments AS Balance FROM finance;

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

Access) SELECT firstName FROM students WHERE firstName LIKE ‘F*’;


MySQL) SELECT firstname FROM students WHERE firstname LIKE ‘F%’;
You can also check against a range of values by using the “IN” operator:

SELECT * FROM students WHERE major IN (‘Computer Science’, ‘Information Technology’,

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)

SELECT * FROM students ORDER BY gpa DESC, lastname ASC;

(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.

SELECT COUNT(*) FROM students;


→→ count all students

SELECT SUM(salary) FROM employees;

→→ add all salary

SELECT AVG(age) FROM students;

→→ average age

SELECT MIN(age), MAX(age) FROM students;

→→ show youngest and oldest student 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).

SELECT COUNT(*), majors FROM students GROUP BY majors;

→ (lists all the majors and the number of students enrolled in that major)

SELECT COUNT(*), majors FROM students GROUP BY majors HAVING COUNT(*) > 5;

→ (lists all majors with at least 5 students enrolled in it)

Joining tables
The following steps are required to join tables:

In the SELECT clause, list all fields you want to display


In the FROM clause, list all tables involved in the query
In the WHERE clause, give the condition that will restrict the data to be retrieved to only those rows that
match; that is, you’ll restrict it to the rows that have COMMON values in matching fields.
For example, to show all students and their financial aid data:

SELECT firstName, studentID, withFinancialAid FROM students, bursar WHERE students.studentID =


bursar.studentID;

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:

UPDATE students SET major=’Business’ WHERE studentID=’135-97’;

→ (changes the student’s major to Business for the student whose ID is 135-97 in the students table)

Creating a Table from a Query


The INTO clause in a query can be used to create a table which contains the query results. An example of
the “INTO” command can be found on page 118 of the text. Note: This format is supported by Access,
but not by MySQL or Oracle.

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

You might also like