0% found this document useful (0 votes)
3 views12 pages

Tutorial 2

The document provides an overview of SQL statements for selecting, inserting, updating, and deleting records in a database, specifically focusing on the 'Customers' table. It explains the use of logical operators (AND, OR, NOT), sorting results with ORDER BY, handling NULL values, and the importance of the WHERE clause in update and delete operations. Additionally, it covers the syntax for inserting new records and retrieving a limited number of records using the SELECT TOP clause.

Uploaded by

santanusantu72
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Tutorial 2

The document provides an overview of SQL statements for selecting, inserting, updating, and deleting records in a database, specifically focusing on the 'Customers' table. It explains the use of logical operators (AND, OR, NOT), sorting results with ORDER BY, handling NULL values, and the importance of the WHERE clause in update and delete operations. Additionally, it covers the syntax for inserting new records and retrieving a limited number of records using the SELECT TOP clause.

Uploaded by

santanusantu72
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Example

SELECT * FROM Customers


WHERE Country='Germany' AND City='Berlin';

OR Example

The following SQL statement selects all fields from "Customers" where city is "Berlin" OR "München":

Example
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';

The following SQL statement selects all fields from "Customers" where country is "Germany" OR
"Spain":

Example
SELECT * FROM Customers
WHERE Country='Germany' OR Country='Spain';

NOT Example

The following SQL statement selects all fields from "Customers" where country is NOT "Germany":

Example
SELECT * FROM Customers
WHERE NOT Country='Germany';

Combining AND, OR and NOT

You can also combine the AND, OR and NOT operators.

The following SQL statement selects all fields from "Customers" where country is "Germany" AND city
must be "Berlin" OR "München" (use parenthesis to form complex expressions):

Example
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');

The following SQL statement selects all fields from "Customers" where country is NOT "Germany" and
NOT "USA":
Example
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';

SQL ORDER BY Keyword


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.

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

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

Customer Customer Customer Address City Postal Country


ID Name Name Code

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste

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


Emparedados Constitución D.F.
y helados 2222

3 Antonio Antonio Mataderos México 05023 Mexico


Moreno Moreno 2312 D.F.
Taquería

4 Around the Thomas 120 Hanover London WA1 UK


Horn Hardy Sq. 1DP

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


snabbköp Berglund 8
ORDER BY Example

The following SQL statement selects all customers from the "Customers" table, sorted by the "Country"
column:

Example
SELECT * FROM Customers
ORDER BY Country;

ORDER BY DESC Example

The following SQL statement selects all customers from the "Customers" table, sorted DESCENDING by
the "Country" column:

Example
SELECT * FROM Customers
ORDER BY Country DESC;

ORDER BY Several Columns Example

The following SQL statement selects all customers from the "Customers" table, sorted by the "Country"
and the "CustomerName" column. This means that it orders by Country, but if some rows have the same
Country, it orders them by CustomerName:

Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;

ORDER BY Several Columns Example 2

The following SQL statement selects all customers from the "Customers" table, sorted ascending by the
"Country" and descending by the "CustomerName" column:

Example
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;

SQL INSERT INTO Statement


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, ...);

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

Customer Customer Customer Address City Postal Country


ID Name Name Code

1 White Clover Karl Jablonski 305 - 14th Seattle 98128 USA


Markets Ave. S. Suite
3B
2 Wilman Kala Matti Keskuskatu 45 Helsinki 21240 Finland
Karttunen

3 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland

INSERT INTO Example

The following SQL statement inserts a new record in the "Customers" table:

Example
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

The selection from the "Customers" table will now look like this:

Customer Customer Customer Address City Postal Country


ID Name Name Code

1 White Clover Karl 305 - 14th Seattle 98128 USA


Markets Jablonski Ave. S. Suite
3B
2 Wilman Kala Matti Keskuskatu 45 Helsinki 21240 Finland
Karttunen

3 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland

4 Cardinal Tom B. Skagen 21 Stavange 4006 Norway


Erichsen r

Did you notice that we did not insert any number into the CustomerID field?
The CustomerID column is an auto-increment field and will be generated automatically when a new
record is inserted into the table.

Insert Data Only in Specified Columns

It is also possible to only insert data in specific columns.

The following SQL statement will insert a new record, but only insert data in the "CustomerName",
"City", and "Country" columns (CustomerID will be updated automatically):

Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');

The selection from the "Customers" table will now look like this:

Customer Customer Customer Address City Postal Country


ID Name Name Code

1 White Clover Karl 305 - 14th Seattle 98128 USA


Markets Jablonski Ave. S. Suite
3B
2 Wilman Kala Matti Keskuskatu 45 Helsinki 21240 Finland
Karttunen

3 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland

4 Cardinal Null Null Stavange 4006 Norway


r

SQL NULL Values


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.

Note: A NULL value is different from a zero value or a field that contains spaces. A field with a NULL
value is one that has been left blank during record creation!

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.

IS 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;

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:
CustomerID CustomerNam CustomerName Address City PostalCod Country
e e

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste

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


Emparedados y Constitución D.F.
helados 2222

3 Antonio Moreno Antonio Moreno Mataderos México 05023 Mexico


Taquería 2312 D.F.

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


Horn Sq.

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


snabbköp Berglund n8

The IS NULL Operator

The IS NULL operator is used to test for empty values (NULL values).

The following SQL lists all customers with a NULL value in the "Address" field:

Example

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NULL;

Tip: Always use IS NULL to look for NULL values.

The IS NOT NULL Operator

The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).

The following SQL lists all customers with a value in the "Address" field:

Example
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
SQL UPDATE Statement
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;

Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement.
The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all
records in the table will be updated!

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

CustomerID CustomerNam CustomerName Address City PostalCod Country


e e

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste

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


Emparedados y Constitución D.F.
helados 2222

3 Antonio Moreno Antonio Moreno Mataderos México 05023 Mexico


Taquería 2312 D.F.

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


Horn Sq.

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


snabbköp Berglund n8
UPDATE Table

The following SQL statement updates the first customer (CustomerID = 1) with a new contact
person and a new city.

Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

The selection from the "Customers" table will now look like this:

CustomerID CustomerNam CustomerName Address City PostalCod Country


e e

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste

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


Emparedados y Constitución D.F.
helados 2222

3 Antonio Moreno Antonio Moreno Mataderos México 05023 Mexico


Taquería 2312 D.F.

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


Horn Sq.

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


snabbköp Berglund n8

UPDATE Multiple Records

It is the WHERE clause that determines how many records will be updated.

The following SQL statement will update the ContactName to "Juan" for all records where country is
"Mexico":
Example
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';

The selection from the "Customers" table will now look like this:

CustomerID CustomerNam CustomerName Address City PostalCod Country


e e

1 Alfreds Juan Obere Str. 57 Berlin 12209 Germany


Futterkiste

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


Emparedados y Constitución D.F.
helados 2222

3 Antonio Moreno Juan Mataderos México 05023 Mexico


Taquería 2312 D.F.

4 Around the Juan 120 Hanover London WA1 1DP UK


Horn Sq.

5 Berglunds Juan Berguvsväge Luleå S-958 22 Sweden


snabbköp n8

SQL DELETE Statement


The SQL DELETE Statement

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

DELETE Syntax
DELETE FROM table_name WHERE condition;
Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement.
The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause, all
records in the table will be deleted!

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

CustomerID CustomerNam CustomerName Address City PostalCod Country


e e

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste

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


Emparedados y Constitución D.F.
helados 2222

3 Antonio Moreno Antonio Moreno Mataderos México 05023 Mexico


Taquería 2312 D.F.

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


Horn Sq.

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


snabbköp Berglund n8

SQL DELETE Example

The following SQL statement deletes the customer "Alfreds Futterkiste" from the "Customers" table:

Example

DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';

The "Customers" table will now look like this:

Customer Customer Customer Address City Postal Country


ID Name Name Code
2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico
Emparedados y Constitución D.F.
helados 2222

3 Antonio Antonio Mataderos México 05023 Mexico


Moreno Moreno 2312 D.F.
Taquería

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


Horn Sq.

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


snabbköp Berglund 8

Delete All Records

It is possible to delete all rows in a table without deleting the table. This means that the table
structure, attributes, and indexes will be intact:

DELETE FROM table_name;

The following SQL statement deletes all rows in the "Customers" table, without deleting the table:

Example

DELETE FROM Customers;

SQL TOP, LIMIT, FETCH FIRST or ROWNUM Clause


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.

You might also like