UU-COM-4008 Reading Material Week 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

UU-COM-4008 –

Databases

Week 3 – Structured Query Language (SQL)


3.1 – SQL
SQL is the standard language used for storing, manipulating and retrieving data when working
with relational (but also other) databases. Furthermore:

 SQL was developed by the IBM Corporation in the late 1970s.


 SQL was endorsed as a United States national standard by the American National
Standards Institute (ANSI) in 1992 [SQL-92].
 A newer version [SQL3] was created and incorporates some object-oriented concepts.
 Other revisions were made through the years. The latest standard, as of May 2017, is
SQL 2016.
 SQL is not a full featured programming language as are C, C#, and Java.
 SQL is a data sublanguage for creating and processing database data, consider it as a
kind of scripting language.
 SQL exists in all enterprise-class DBMS products therefore SQL programming is a
critical skill if you want to pursue a career in IT
 SQL statements can be logically split into two categories:

• Data manipulation language (DML) statements: there are used for


queries and data modification.

• Data definition language (DDL) statements: these are used for creating
tables, relationships, and other structures.

We will start by learning DML and with the SELECT statement. The SELECT statement is used
for retrieving data from tables. Note that white space is ignored in SQL, and capitalisation also
doesn’t matter. SELECT has the following format:

– SELECT ColumnName(s)

– FROM TableName(s)

– WHERE Conditions;

Note that each DBMS product has a way/interface to run SQL commands. In Microsoft
Access, you create a query, then go to SQL view, then enter the SQL statements. It’s
important to actually test out the SQL statements and run the examples yourself. If you
are unable to install a database product, you can go to the link below where you can run
SQL statements, on their own sample database, without installing anything:

– https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all

UU-COM-4008 - Databases Page 15


UU-COM-4008 –
Databases
Now, we are going to use a sample database (found on VLE) for a store that sells outdoor
equipment, for our examples that has the following tables (and structure):

So, using the above let’s see a few examples:

Example 1: SELECT Buyer, Department FROM SKU_DATA;

This statement would retrieve the department and buyer fields from the SKU_DATA table.
Have a look at the following snapshot:

Notice how we have repeating results, this is because each buyer bought multiple items from
each department.

Example 2: SELECT DISTINCT Buyer, Department FROM SKU_DATA;

By adding the DISTINCT keyword, duplicate results are eliminated from view.

UU-COM-4008 - Databases Page 16


UU-COM-4008 –
Databases
Example 3:

SELECT *

FROM SKU_DATA

WHERE Department = 'Water Sports';

Here we see that we can use the NOTE: SQL wants plain ASCII single quotes when

Example 4:

SELECT *

FROM ORDER_ITEM

ORDER BY Price DESC, OrderNumber ASC;

Here we can see that the ORDER BY keywords are used, which allow us to sort the returned
results first by Price in Descending order and secondly by OrderNumber in Ascending order.

Example 5:

SELECT *

FROM SKU_DATA

WHERE Department = 'Water Sports' AND Buyer = 'Nancy Meyers';

Here notice that in the where clause we have added the AND keyword. This works likes
Boolean AND, where both conditions have to be met in order for results to be produced.
Therefore the query will return results only if the Nancy bought something from the Water
Sports department.

Keep in mind that OR can be used instead of AND, i.e. results will show if Nancy bought
something from any department or if the department is water sports (or both).

Example 6:

SELECT *

FROM SKU_DATA

WHERE Buyer IN ('Nancy Meyers','Cindy Lo', 'Jerry Martin');

In this example the keyword IN is used. This will return results if the buyer is one (or two or
even all three of them) of the people in brackets. In this case we could have re-written the

UU-COM-4008 - Databases Page 17


UU-COM-4008 –
Databases
statement with buyer = and 2 OR keywords. Note that NOT IN can also be used to exclude
results.

Example 7:

SELECT *

FROM ORDER_ITEM

WHERE ExtendedPrice BETWEEN 100 AND 200;

This example shows us how the keyword BETWEEN is used, in order to limit the retrieved
results within a numerical range. Alternatively the same data can be retrieved by using the >=
and <= symbols as shown below:

SELECT *

FROM ORDER_ITEM

WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200;

Example 8:

SELECT *

FROM SKU_DATA

WHERE Buyer LIKE 'Pete%';

Here we see how to do wildcard searches. The % symbol is used to represent any number of
wildcard characters. This means that our buyer should start with Pete, and then anything can
follow afterwards. In our sample database this is what you will get:

Similarly, if you were to use the % before and after Pete, it would essentially mean that Pete
has to be in the middle of a word (i.e. something has to follow before and after).

Now it’s time to see some of the built in formulas of SQL that have to do with numbers. There
are keywords that calculate the Total, Average, Minimum and Maximum. These are displayed
in the following example:

UU-COM-4008 - Databases Page 18


UU-COM-4008 –
Databases
Example 9:

SELECT SUM (ExtendedPrice) AS OrderItemSum,

AVG (ExtendedPrice) AS OrderItemAvg,

MIN (ExtendedPrice) AS OrderItemMin,

MAX (ExtendedPrice) AS OrderItemMax

FROM ORDER_ITEM;

This would return the following:

Notice that besides the formulas, we are also using the AS keyword (which is used to rename
the resulting column to whatever name we give, as shown above).

Example 10:

SELECT COUNT(*) AS NumRows

FROM ORDER_ITEM;

Count is used to count the number of rows in a table.

Example 11:

SELECT Quantity * Price AS EP,

ExtendedPrice

FROM ORDER_ITEM;

As you can see in the above example, it’s possible to do any kind of simple mathematical
calculations, by using the calculation symbol (* is for multiplication in this case) and a field.

Example 12:

SELECT Course, COUNT(*)

FROM STUDENT

GROUP BY Course HAVING COUNT(*) > 2

UU-COM-4008 - Databases Page 19


UU-COM-4008 –
Databases
The above example is from a different database. It introduces the GROUP BY keyword. It will
return and group the results by Course and then show the count (number of students in each
course).

Example 13:

SELECT SUM (ExtendedPrice) AS Revenue

FROM ORDER_ITEM

WHERE SKU IN

(SELECT SKU

FROM SKU_DATA

WHERE Department = 'Water Sports');

Here we see a statement with a subquery. The subquery is executed first, so it will find the
SKU’s, which will then be used a search criteria for the main query. This will return the sum of
the “extendedprices” for the sales of the Water Sports department.

Example 14:

SELECT Buyer, ExtendedPrice, OrderMonth

FROM SKU_DATA, ORDER_ITEM, RETAIL_ORDER

WHERE SKU_DATA.SKU = ORDER_ITEM.SKU

AND ORDER_ITEM.OrderNumber =

RETAIL_ORDER.OrderNumber;

Here we see how we compare the results of tables based on their key attributes. Firstly, we
check to retrieve only results that have the same SKU between the SKU_DATA and
ORDER_ITEM tables AND in addition the same OrderNumber between the ORDER_ITEM and
RETAIL_ORDER tables.

The above examples conclude our discussion on SELECT. We will now see examples of the
other three DML commands: INSERT, UPDATE and DELETE. INSERT is used to enter new
records in tables. UPDATE is used to update existing tables. DELETE is used to delete records
from tables.

UU-COM-4008 - Databases Page 20


UU-COM-4008 –
Databases
Example 15:

INSERT INTO ARTIST (Name, Nationality, Birthdate, DeceasedDate)

VALUES ('Tamayo', 'Mexican', 1927, 1998);

The above statement will insert in the artist table, in the respective columns (which should
already exist and be in the appropriate format, the values in brackets. Notice how numerical
values need no quotes.

Example 16:

INSERT INTO ARTIST (Name, Nationality, Birthdate)

SELECT Name, Nationality, Birthdate

FROM IMPORTED_ARTIST;

The above statement is used for a bulk insert. That is, it will first retrieve the values from the
imported_artist table, and put them in the artist table. Both tables should pre-exist and the
structure should be the same.

Example 17:

UPDATE CUSTOMER

SET City = 'New York City'

WHERE CustomerID = 1000;

The above query will update the customer table by changing the city to New York City, for the
customer with ID equal to 1000. (this could be used if the said customer moved to New York).

Example 18:

UPDATE CUSTOMER

SET AreaCode = '333'

WHERE City = 'Denver';

The above query will change the areacode for all customers living in Denver to 333.

Example 19:

DELETE FROM CUSTOMER

WHERE CustomerID = 1000;

UU-COM-4008 - Databases Page 21


UU-COM-4008 –
Databases
This simply deletes the record of customer with id 1000 from the customer table. Bulk delete
can be done by omitting or modifying the where clause as appropriate.

We will now proceed to DDL which has the following commands:

CREATE – used to create a new table, database or schema


ALTER – used to alter an existing table or column description
DROP – used to delete existing objects from the database

Let’s see examples of the above:

Example 20:

Create Table Artist (ArtistID Number Primary Key, Name Text(30) Unique, Birthdate Date
NOT Null)

The above statement will create a new table with the respective column names and data
types / restrictions. Note that each DBMS has its own keywords that you can use. For
example, the above statement works in Microsoft Access.

Example 21:

CREATE TABLE employees

( last_name VARCHAR(50) NOT NULL,

first_name VARCHAR(50) NOT NULL, salary MONEY,

CONSTRAINT employees_pk PRIMARY KEY (last_name, first_name)

);

The above example would not work in Microsoft Access, but does work in Microsoft SQL
Server. Notice that the data types are different; here we have varchar with length 50. We are
also using the Money data type. We are also creating a constraint with the form of a primary
key (the name of the constraint is employees_pk) that specifies the PK to be a combination of
last and first name (composite PK). By having a name, it makes it easier to
handle/modify/delete the PK. You have to look at the specific DBMS you are using, find it’s
exact SQL engine then look it up online or in books to find the exact keywords used in each
engine. For example, MS Access uses JetSQL while MS SQL server uses T-SQL and Oracle
PL/SQL.

Example 22:

ALTER TABLE CUSTOMER ADD LastName Char(50) NULL;

ON UPDATE CASCADE ON DELETE NO ACTION;

UU-COM-4008 - Databases Page 22


UU-COM-4008 –
Databases
Here we see that the Alter statement is used to add a column called LastName (length 50 and
can accept NULL) to the Customer table. In addition, if the parent primary key is changed
(assuming there is a related foreign key value in the referenced table), the child value will also
change to reflect that (cascade update). On delete no action means that the database will
reject the delete operation for the parent table if there is a related foreign key value in the
referenced table.

Example 23:

ALTER TABLE CUSTOMER DROP COLUMN MyColumn;

ALTER TABLE CUSTOMER DROP CONSTRAINT MyConstraint;

The first line will delete MyColumn from the customer table.

The second case will drop the constraint.

Example 24:

DROP CONSTRAINT TransactionCustomerFK;

DROP TABLE CUSTOMER;

The above examples use the drop statement to permanently delete a constraint in the first
case and a table in the second case. Tables cannot be dropped if they have related tables,
constraints etc, these must be dropped first and then the table.

Note: again I’d like to remind you that each DBMS product is using its own SQL version and
many DBMS’s (such as Oracle and SQL Server) have their own extensions to SQL. PL/SQL for
example used by oracle allows you to use programming concepts together with SQL. So, you
first learn how to use SQL, and then you also have to learn additional material on how to use
the specific DBMS product you want to use, or they DBMS that they have/using in your future
employment. In addition, it’s worth noting that there exist a number of certifications that you
can take on specific DBMS products. Oracle for instance has 15 different certifications on
Oracle version 12c alone (latest version as of 2017). Certifications are obtained by studding
material then passing exams set by Oracle (and also paying significant fees). Once obtained
they can add even further business value to your person and can increase your chances of
being hired and can also boost your career to new heights. These are usually done if a
company requires it (in which case they will probably pay for them) or for an attempt to self-
progress yourself or certify your expertise.

UU-COM-4008 - Databases Page 23

You might also like