0% found this document useful (0 votes)
26 views

Positivism: Rowan N. Elomina

The document provides information about SQL (Structured Query Language). It discusses that SQL is a standard language for storing, manipulating, and retrieving data from databases. It also describes several common SQL statements like SELECT, INSERT, WHERE and ORDER BY and provides syntax examples for each. The document is intended to teach the basics of SQL.

Uploaded by

r0wan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Positivism: Rowan N. Elomina

The document provides information about SQL (Structured Query Language). It discusses that SQL is a standard language for storing, manipulating, and retrieving data from databases. It also describes several common SQL statements like SELECT, INSERT, WHERE and ORDER BY and provides syntax examples for each. The document is intended to teach the basics of SQL.

Uploaded by

r0wan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 85

Positivism

Rowan N. Elomina
PHDBM 711 - Philosophical Foundation of Management
SQL

PHDBM 711 - Philosophical Foundation of Management 1-2


SQL

• SQL is a standard language for storing, manipulating and retrieving data in


databases.

• Our SQL tutorial will teach you how to use SQL in: MySQL, SQL Server, MS Access,
Oracle, Sybase, Informix, Postgres, and other database systems.

PHDBM 711 - Philosophical Foundation of Management 1-3


Introduction to SQL

PHDBM 711 - Philosophical Foundation of Management 1-4


SQL is a standard language for accessing and manipulating
databases.

PHDBM 711 - Philosophical Foundation of Management 1-5


What is SQL?

• SQL stands for Structured Query Language


• SQL lets you access and manipulate databases
• SQL became a standard of the American National Standards Institute (ANSI) in 1986,
and of the International Organization for Standardization (ISO) in 1987

PHDBM 711 - Philosophical Foundation of Management 1-6


What Can SQL do?

PHDBM 711 - Philosophical Foundation of Management 1-7


• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database

PHDBM 711 - Philosophical Foundation of Management 1-8


• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views

PHDBM 711 - Philosophical Foundation of Management 1-9


SQL is a Standard - BUT....

PHDBM 711 - Philosophical Foundation of Management 1-10


Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.

However, to be compliant with the ANSI standard, they all support at least the major commands
(such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.

PHDBM 711 - Philosophical Foundation of Management 1-11


Using SQL in Your Web Site
To build a web site that shows data from a database, you will need:

PHDBM 711 - Philosophical Foundation of Management 1-12


• An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
• To use a server-side scripting language, like PHP or ASP
• To use SQL to get the data you want
• To use HTML / CSS to style the page

PHDBM 711 - Philosophical Foundation of Management 1-13


RDBMS

PHDBM 711 - Philosophical Foundation of Management 1-14


• RDBMS stands for Relational Database Management System.
• RDBMS is the basis for SQL, and for all modern database systems
such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft
Access.
• The data in RDBMS is stored in database objects called tables. A
table is a collection of related data entries and it consists of
columns and rows.

PHDBM 711 - Philosophical Foundation of Management 1-15


SQL Syntax

PHDBM 711 - Philosophical Foundation of Management 1-16


Database Tables

• A database most often contains one or more tables. Each table is identified by a
name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.

• In this tutorial we will use the well-known Northwind sample database (included in
MS Access and MS SQL Server).

• Below is a selection from the "Customers" table:

PHDBM 711 - Philosophical Foundation of Management 1-17


CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedados y Constitución 2222
helados

3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería

4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

5 Berglunds Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden


snabbköp

PHDBM 711 - Philosophical Foundation of Management 1-18


SQL SELECT Statement

PHDBM 711 - Philosophical Foundation of Management 1-19


The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.

PHDBM 711 - Philosophical Foundation of Management 1-20


SELECT Syntax

SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the field names of the table you want to select data from. If you want to
select all the fields available in the table, use the following syntax:

SELECT * FROM table_name;

PHDBM 711 - Philosophical Foundation of Management 1-21


Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:
CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedados y Constitución
helados 2222
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería

4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

5 Berglunds Christina Berguvsvägen 8 Luleå S-958 22 Sweden


snabbköp Berglund

PHDBM 711 - Philosophical Foundation of Management 1-22


SQL SELECT DISTINCT Statement

PHDBM 711 - Philosophical Foundation of Management 1-23


The SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only
want to list the different (distinct) values.

PHDBM 711 - Philosophical Foundation of Management 1-24


SELECT DISTINCT Syntax

SELECT DISTINCT column1, column2, ...
FROM table_name;

PHDBM 711 - Philosophical Foundation of Management 1-25


SQL WHERE Clause

PHDBM 711 - Philosophical Foundation of Management 1-26


The SQL WHERE Clause
The WHERE clause is used to filter records.

It is used to extract only those records that fulfill a specified condition.

PHDBM 711 - Philosophical Foundation of Management 1-27


WHERE Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;

PHDBM 711 - Philosophical Foundation of Management 1-28


SQL AND, OR and NOT Operators

PHDBM 711 - Philosophical Foundation of Management 1-29


The SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one
condition:

The AND operator displays a record if all the conditions separated by AND are TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.

PHDBM 711 - Philosophical Foundation of Management 1-30


Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
AND

PHDBM 711 - Philosophical Foundation of Management 1-31


SQL ORDER BY Keyword

PHDBM 711 - Philosophical Foundation of Management 1-32


The SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.

PHDBM 711 - Philosophical Foundation of Management 1-33


ORDER BY Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

PHDBM 711 - Philosophical Foundation of Management 1-34


SQL INSERT INTO Statement

PHDBM 711 - Philosophical Foundation of Management 1-35


The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);
2. If you are adding values for all the columns of the table, you do not need to specify the column names in the
SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the
INSERT INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

PHDBM 711 - Philosophical Foundation of Management 1-36


SQL NULL Values

PHDBM 711 - Philosophical Foundation of Management 1-37


What is a NULL Value?
A field with a NULL value is a field with no value.

If a field in a table is optional, it is possible to insert a new record or update a record


without adding a value to this field. Then, the field will be saved with a NULL value.

PHDBM 711 - Philosophical Foundation of Management 1-38


How to Test for NULL Values?
It is not possible to test for NULL values with comparison operators, such as =, <, or
<>.

We will have to use the IS NULL and IS NOT NULL operators instead.

PHDBM 711 - Philosophical Foundation of Management 1-39


 NULL Syntax

SELECT column_names

FROM table_name

WHERE column_name IS NULL;

IS NOT NULL Syntax

SELECT column_names

FROM table_name

WHERE column_name IS NOT NULL;

PHDBM 711 - Philosophical Foundation of Management 1-40


SQL UPDATE Statement

PHDBM 711 - Philosophical Foundation of Management 1-41


The SQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

PHDBM 711 - Philosophical Foundation of Management 1-42


SQL DELETE Statement

PHDBM 711 - Philosophical Foundation of Management 1-43


The SQL DELETE Statement
The DELETE statement is used to delete existing records in a table.

DELETE Syntax

DELETE FROM table_name WHERE condition;

PHDBM 711 - Philosophical Foundation of Management 1-44


SQL TOP, LIMIT, FETCH FIRST or ROWNUM Clause

PHDBM 711 - Philosophical Foundation of Management 1-45


The SQL SELECT TOP Clause
The SELECT TOP clause is used to specify the number of records to return.

The SELECT TOP clause is useful on large tables with thousands of records. Returning a
large number of records can impact performance.

PHDBM 711 - Philosophical Foundation of Management 1-46


QL Server / MS Access Syntax:

SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;

MySQL Syntax:

SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;

PHDBM 711 - Philosophical Foundation of Management 1-47


acle 12 Syntax:

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY;

Older Oracle Syntax:

SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;

PHDBM 711 - Philosophical Foundation of Management 1-48


Older Oracle Syntax (with ORDER BY):

SELECT *
FROM (SELECT column_name(s) FROM table_name ORDER BY column_name(s))
WHERE ROWNUM <= number;

PHDBM 711 - Philosophical Foundation of Management 1-49


SQL MIN() and MAX() Functions

PHDBM 711 - Philosophical Foundation of Management 1-50


The SQL MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

PHDBM 711 - Philosophical Foundation of Management 1-51


MIN() Syntax

SELECT MIN(column_name)
FROM table_name
WHERE condition;

MAX() Syntax

SELECT MAX(column_name)
FROM table_name
WHERE condition;

PHDBM 711 - Philosophical Foundation of Management 1-52


SQL COUNT(), AVG() and SUM() Functions

PHDBM 711 - Philosophical Foundation of Management 1-53


The SQL COUNT(), AVG() and SUM() Functions
The COUNT() function returns the number of rows that matches a specified criterion.

PHDBM 711 - Philosophical Foundation of Management 1-54


COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
The AVG() function returns the average value of a numeric column. 
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
The SUM() function returns the total sum of a numeric column. 
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;

PHDBM 711 - Philosophical Foundation of Management 1-55


SQL LIKE Operator

PHDBM 711 - Philosophical Foundation of Management 1-56


The SQL LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.

There are two wildcards often used in conjunction with the LIKE operator:

 The percent sign (%) represents zero, one, or multiple characters


 The underscore sign (_) represents one, single character

PHDBM 711 - Philosophical Foundation of Management 1-57


Here are some examples showing different LIKE operators with '%' and '_' wildcards:
LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in
length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in
length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

PHDBM 711 - Philosophical Foundation of Management 1-58


SQL Wildcards

PHDBM 711 - Philosophical Foundation of Management 1-59


SQL Wildcard Characters
A wildcard character is used to substitute one or more characters in a string.

Wildcard characters are used with the LIKE operator. The LIKE operator is used in a
WHERE clause to search for a specified pattern in a column.

PHDBM 711 - Philosophical Foundation of Management 1-60


Wildcard Characters in MS Access

Symbol Description Example

* Represents zero or more characters bl* finds bl, black, blue, and blob

? Represents a single character h?t finds hot, hat, and hit

[] Represents any single character within h[oa]t finds hot and hat, but not hit
the brackets

! Represents any character not in the h[!oa]t finds hit, but not hot and hat
brackets

- Represents a range of characters c[a-b]t finds cat and cbt

# Represents any single numeric character 2#5 finds 205, 215, 225, 235, 245, 255,
265, 275, 285, and 295

PHDBM 711 - Philosophical Foundation of Management 1-61


Wildcard Characters in SQL Server
Symbol Description Example

% Represents zero or more characters bl% finds bl, black, blue, and blob

_ Represents a single character h_t finds hot, hat, and hit

[] Represents any single character within h[oa]t finds hot and hat, but not hit
the brackets

^ Represents any character not in the h[^oa]t finds hit, but not hot and hat
brackets

- Represents a range of characters c[a-b]t finds cat and cbt

PHDBM 711 - Philosophical Foundation of Management 1-62


All the wildcards can also be used in combinations!
Here are some examples showing different LIKE operators with '%' and '_' wildcards:

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that starts with "a"

WHERE CustomerName LIKE '%a' Finds any values that ends with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%_%' Finds any values that starts with "a" and are at least 3
characters in length

WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" and ends with "o"

PHDBM 711 - Philosophical Foundation of Management 1-63


SQL IN Operator

PHDBM 711 - Philosophical Foundation of Management 1-64


The SQL IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

PHDBM 711 - Philosophical Foundation of Management 1-65


IN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

PHDBM 711 - Philosophical Foundation of Management 1-66


SQL BETWEEN Operator

PHDBM 711 - Philosophical Foundation of Management 1-67


The SQL BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

PHDBM 711 - Philosophical Foundation of Management 1-68


BETWEEN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

PHDBM 711 - Philosophical Foundation of Management 1-69


SQL Aliases

PHDBM 711 - Philosophical Foundation of Management 1-70


SQL Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.

Aliases are often used to make column names more readable.

An alias only exists for the duration of that query.

An alias is created with the AS keyword.

PHDBM 711 - Philosophical Foundation of Management 1-71


Alias Column Syntax

SELECT column_name AS alias_name
FROM table_name;

Alias Table Syntax

SELECT column_name(s)
FROM table_name AS alias_name;

PHDBM 711 - Philosophical Foundation of Management 1-72


SQL Joins

PHDBM 711 - Philosophical Foundation of Management 1-73


SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related column between
them.
Let's look at a selection from the "Orders" table:
OrderID CustomerID OrderDate

10308 2 1996-09-18

10309 37 1996-09-19

10310 77 1996-09-20

PHDBM 711 - Philosophical Foundation of Management 1-74


Then, look at a selection from the "Customers" table:
Customer ID CustomerName ContactName Country

1 Alfreds Futterkiste Maria Anders Germany

2 Ana Trujillo Emparedados y Ana Trujillo Mexico


helados

3 Antonio Moreno Taquería Antonio Moreno Mexico

Notice that the "Customer ID" column in the "Orders" table refers to the "Customer ID" in the "Customers" table. The relationship between the two tables
above is the "Customer ID" column.

PHDBM 711 - Philosophical Foundation of Management 1-75


SQL INNER JOIN Keyword

PHDBM 711 - Philosophical Foundation of Management 1-76


SQL INNER JOIN Keyword

The INNER JOIN keyword selects records that have matching values in both tables.
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

PHDBM 711 - Philosophical Foundation of Management 1-77


SQL LEFT JOIN Keyword

PHDBM 711 - Philosophical Foundation of Management 1-78


SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from the right
side, if there is no match.

PHDBM 711 - Philosophical Foundation of Management 1-79


LEFT JOIN Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

PHDBM 711 - Philosophical Foundation of Management 1-80


SQL RIGHT JOIN Keyword

PHDBM 711 - Philosophical Foundation of Management 1-81


SQL RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all records from the right table (table2), and the
matching records from the left table (table1). The result is 0 records from the left side,
if there is no match.

RIGHT JOIN Syntax

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

PHDBM 711 - Philosophical Foundation of Management 1-82


SQL FULL OUTER JOIN Keyword

PHDBM 711 - Philosophical Foundation of Management 1-83


SQL FULL OUTER JOIN Keyword

The FULL OUTER JOIN keyword returns all records when there is a match in left
(table1) or right (table2) table records.

Tip: FULL OUTER JOIN and FULL JOIN are the same.

PHDBM 711 - Philosophical Foundation of Management 1-84


FULL OUTER JOIN Syntax

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

PHDBM 711 - Philosophical Foundation of Management 1-85

You might also like