46
Introduction to SQL Prepared by: Romer Ian O. Pasoc

Intro to SQL

Embed Size (px)

DESCRIPTION

Structures query language

Citation preview

Introduction to SQLPrepared by: Romer Ian O. Pasoc

What is SQL?

• SQL stands for Structured Query Language.

• It is the most widely used commercial relational database language.

• Now the de facto standard for DBMS products by ANSI (American National Standards Institute).

• SQL continues to evolve in response to changing need in the database era.

What is SQL

• SQL lets you access and manipulate databases

What Can SQL Do?

• 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;• SQL can create new databases;• SQL can create new tables in a database;

Some of the Most Important SQL Commands

• SELECT• UPDATE• DELETE• INSERT INTO• CREATE DATABASE• ALTER DATABASE• CREATE TABLE• ALTER TABLE• DROP TABLE

SQL Functions

• DDL (Data Definition Language)Function of SQL that supports the creation, deletion, and modification of definitions for tables. The DDL also provides commands for specifying access rights or privileges to tables.

• DML (Data Manipulation Language)Function of SQL that allows users to pose queries and to insert, delete, and modify rows/records.

Data Definition

Creation of the schema or overall structure of the database.

Two task must be completed:

-Create database structure

-Create tables that will hold end-user data

The Database Schema

• AUTHENTICATION- DBMS verifies that only registered users are

able to access database- Log on using the user ID and password

created by database administrator

• SCHEMA- Group of database objects that are related to

each other

DDL Commands

• CREATEThe CREATE command is used to establish databases and tables in your DBMS.

• ALTEROnce you have created a table within a database, you may wish to modify the definition of it. The ALTER command allows you to make changes to the structure of a table without deleting and recreating it.

DDL Commands

• DROP

The final command of the DDL, DROP, allows us to remove entire database object (table or database) from the DBMS.

Data Manipulation

These commands will be used by all database users during the routine operation of the database.

DML Commands

• SELECTThe SELECT command is the most commonly used command in SQL. It allows users to retrieve the specific information they desire from the database.

• INSERTThe INSERT command is used to add records to an existing table.

DML Commands

• UPDATE

The UPDATE command can be used to modify information contained within a table, either in bulk or individually.

• DELETE

The DELETE command is used to delete records from specified tables.

SQL Statements

• Most of the actions you need to perform on a database are done with SQL Statements.

• Every SQL command has its corresponding SQL statement.

• SQL is not case-sensitive; but for clarity, SQL keywords will be written in UPPER-CASE.

• We will be using an imaginary database called “Northwind”. A database that holds customer data.

“Customers” TableCustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste

Maria Anders Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo Avda. de la Constitución 2222

México D.F. 05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno Mataderos 2312

México D.F. 05023 Mexico

4 Around the Horn

Thomas Hardy 120 Hanover Sq.

London WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

SQL SELECT Statement

SELECT

• The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set.

• Syntax:

SELECT column_name1, column_name2,…column_nameNFROM table_name

-OR-

SELECT * FROM table_name

SELECT

• The asterisk (*) is used here as a means to select all columns of the specified table.

SELECT

Example

SELECT CustomerName, Country FROM Customers

-columns: CustomerName and Country

-Table: Customers

SELECT

Result – Set

CustomerName Country

Alfreds Futterkiste Germany

Ana Trujillo Emparedados y helados Mexico

Antonio Moreno Taquería Mexico

Around the Horn UK

Berglunds snabbköp Sweden

SELECT

Example

SELECT * FROM Customers

-columns: all columns

-Table: Customers

SELECT

• Result – SetCustomerID CustomerNa

meContactName Address City PostalCode Country

1 Alfreds Futterkiste

Maria Anders Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo Avda. de la Constitución 2222

México D.F. 05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F. 05023 Mexico

4 Around the Horn

Thomas Hardy

120 Hanover Sq.

London WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå S-958 22 Sweden

SELECT DISTINCT

• In a table, a column may contain many duplicate values; and sometimes you only want to list the different values.

• SELECT DISTINCT statement is used to return only distinct (different/unique) values.

SELECT DISTINCT

• Syntax:

SELECT DISTINCT colum_name1,column_name2…column_nameN

FROM table_name

SELECT DISTINCT

• Example:

SELECT DISTINCT City FROM Customers

SELECT DISTINCT

• Result - Set

• Copies of Mexico D. F. were omitted.

City

Berlin

México D.F.

London

Luleå

SQL WHERE Clause

• The WHERE clause is used to extract only records that fulfill a specified criterion.

• It is not limited only to selection but also is applicable to any query that requires specific record to handle.

SQL WHERE Clause

• Syntax:SELECT columnsFROM table_nameWHERE column_name operator value

• Column_name after the WHERE word refers to what column will the selection depends NOT the columns to display.

• Operator could be any arithmetic or logical operator

• Value will be the specified value in that column

SQL WHERE Clause

• Example

SELECT * FROM Customers

WHERE Country = ‘Mexico’

The above statement will return the record of all columns from the table Customers where the value of Country column is Mexico

SQL WHERE Clause

Text Fields vs Numeric Fields

SQL requires single quotes around text type/values. Numeric fields: fields that have numeric datatype; should NOT be enclosed in quotes.

SQL WHERE Clause

Result – SetCustomerID CustomerName ContactName Address City PostalCode Country

2 Ana Trujillo Emparedados y helados

Ana Trujillo Avda. de la Constitución 2222

México D.F. 05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F. 05023 Mexico

Operators in the WHERE Clause

Operator Description

= Equal

<> Not equal. Note: In some versions of SQL this operator may be written as !=

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

BETWEEN Between an inclusive range

LIKE Search for a pattern

IN To specify multiple possible values for a column

SQL AND and OR Operators

• The AND operator displays a record if both the first and the second condition are true.

• The OR operator displays a record if either the first or the second condition is true

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

SQL AND and OR Operators

Example SELECT * FROM CustomersWHERE Country = ‘Germany’ AND City = ‘Berlin’

•The above query will return all records with all columns from Customers table where its country is Germany AND city is Berlin

SQL AND and OR Operators

Result – Set

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin 12209 Germany

SQL AND and OR Operators

Example SELECT * FROM CustomersWHERE City = ‘Berlin’ OR City = ‘México D.F.’

•The above query will return all records with all columns from Customers table where its country is Germany AND city is Berlin

SQL AND and OR Operators

Result – Set

CustomerID CustomerName

ContactName Address City PostalCode Country

1 Alfreds Futterkiste

Maria Anders Obere Str. 57

Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo Avda. de la Constitución 2222

México D.F. 05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F. 05023 Mexico

SQL ORDER BY Keyword

• The ORDER BY keyword is used to sort the result-set by one or more columns.

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

SQL ORDER BY Keyword

Syntax:

SELECT columns

FROM table_name

ORDER BY columns ASC/DESC

SQL ORDER BY Keyword

Example:

SELECT CustomerID, CustomerName FROM Customers

ORDER BY City

SQL ORDER BY Keyword

Result – Set

The result-set above will display the columns of all records arranged in ascending order based on its city: Berlin, London, Lulea, Mexico D. F.

CustomerID CustomerName

1 Alfreds Futterkiste

4 Around the Horn

5 Berglunds snabbköp

2 Ana Trujillo Emparedados y helados

3 Antonio Moreno Taquería

SQL ORDER BY Keyword

Example:

SELECT CustomerID, CustomerName FROM Customers

ORDER BY City DESC

SQL ORDER BY Keyword

Result – Set

The result-set above will display the columns of all records arranged in descending order based on its city: Mexico D.F., Lulea, London, Berlin

CustomerID CustomerName

3 Antonio Moreno Taquería

2 Ana Trujillo Emparedados y helados

5 Berglunds snabbköp

4 Around the Horn

1 Alfreds Futterkiste

SQL ORDER BY Keyword

Example:

SELECT CustomerID, CustomerName FROM Customers

ORDER BY Country, ContactName

SQL ORDER BY Keyword

Result – Set

The result-set above will display the columns of all records arranged in ascending order based 1st on its country THEN if they have the same country, will then sort by ContactName.

CustomerID CustomerName

1 Alfreds Futterkiste

2 Ana Trujillo Emparedados y helados

3 Antonio Moreno Taquería

5 Berglunds snabbköp

4 Around the Horn