LESSON 7 - Introduction To SQL For Visual Basic Programming
LESSON 7 - Introduction To SQL For Visual Basic Programming
7
Introduction to SQL
If you want to work with databases, you must learn to speak their language. Databases speak
STRUCTURED QUERY LANGUAGE, better known as SQL, which was invented by E. F.
Codd in the 1970’s.
What is SQL?
• SQL stands for Structured Query Language.
• SQL is a standard computer language for accessing and manipulating databases.
• SQL can retrieve or extract data from a database.
• SQL can insert new records in a database.
• SQL can update and delete records from a database.
• SQL is easy to learn.
SQL encompasses two distinct categories of statements: Data Definition language (DDL)
and Data Manipulation Language (DML). The DDL subset includes a group of statements
that allow you to create database structures, such as tables, fields, indices and so on. The DML
subset includes all the commands that allow you to query and modify the data in the database,
add new records, or delete existing ones. Most of the time, you’ll use only DML statements to
retrieve and update data stored in a database.
7
c. DELETE – deletes data from a database table.
d. INSERT – inserts new data into a database table.
A database most often contains one or more tables. Each table is identified by a name (e.g.
“Customers” or “Orders”). Tables contain records (row) with data.
The table above contains three records (one for each person) and four columns (LastName,
FirstName, Address, City).
The SELECT statement selects columns of data from a database table. The tabular result is
stored in a result table called the resultset.
Syntax:
To select the columns named “LastName” and “FirstName”, use a SELECT statement like this:
The result:
LastName FirstName
Hansen Ola
Stevendson Tove
Pettersen Karl
7
To select all columns from the “Persons” table, use an * (asterisk) symbol instead of column
name, like this:
The result:
LastName FirstName Address City
Hansen Ola Timotein 20 Sandnes
Stevendson Tove Borgvn 23 Sandnes
Pettersen Karl Storgt 20 Stavanger
To conditionally select data from a table, a WHERE clause can be added to the SELECT
statement.
Syntax:
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
Note: In some versions of SQL, the Not Equal operator <> can be written as !=
Example: To select the persons living in the city “Sandnes”, we add a WHERE clause to the
SELECT statement:
The result:
Information and Communication Technology Department 55
Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING
7
LastName FirstName Address City Year
Hansen Ola Timotein 20 Sandnes 1951
Stevendson Tove Borgvn 23 Sandnes 1978
Stevendson Stale Kaivn 18 Sandnes 1980
Syntax:
A “%” sign can be used to define wildcards (missing letters in the pattern) both before and after
the pattern.
Example 1: To extract record(s) with First Names that start with an ‘O’:
Example 2: To retrieved record(s) from Persons table with First Names that end with an ‘a’:
Example 3: To return record(s) of persons with First Names that contains the pattern ‘la’:
The AND operator displays a record if ALL conditions listed evaluates to TRUE. The OR
operator displays a record if ANY of the condition listed evaluates to TRUE.
7
Example 1: Use AND to display each person with the First Name equal to “Tove”, and the Last
Name equal to “Stevendson”:
Result:
LastName FirstName Address City
Stevendson Tove Borgvn 23 Sandnes
Example 2: Use OR to display each person with the First Name equal to “Tove”, or the Last
Name equal to “Stevendson”:
Result:
LastName FirstName Address City
Stevendson Tove Borgvn 23 Sandnes
Stevendson Stephen Kaivn 18 Sandnes
Example 3: You can also combine AND and OR (use parenthesis to form complex
expressions):
The result:
LastName FirstName Address City
Stevendson Tove Borgvn 23 Sandnes
Stevendson Stephen Kaivn 18 Sandnes
The BETWEEN . . . AND operator selects an inclusive range of data between two values.
These values can be numbers, text, or dates.
Syntax:
7
Example 1: To display the persons alphabetically between (and including) “Hansen” and
“Pettersen”, use the following SQL:
Result:
LastName FirstName Address City
Hansen Ola Timotein 20 Sandnes
Nordmann Anna Neset 18 Sandnes
Pettersen Karl Storgt 20 Stavanger
Example 2: To display the persons outside the range used in the previous example, use the
NOT operator:
SELECT * FROM Persons WHERE LastName NOT BETWEEN 'Hansen' AND 'Pettersen'
The result:
LastName FirstName Address City
Stevendson Tove Borgvn 23 Sandnes
Syntax:
7
Example 2: Select Distinct Companies from Orders
To select distinct or unique values from the column named “Company”, we use the DISTINCT
keyword with the SELECT statement:
The result:
Company
Sega
Williams
ABC Shop
SQL ORDER BY
Result:
Company OrderNumber
ABC Shop 5678
Sega 3412
Williams 6798
Williams 2312
Example 2: To display the Companies in alphabetical order and the Order Number in numerical
order:
Result:
Company OrderNumber
ABC Shop 5678
Sega 3412
Williams 2312
Williams 6798
Information and Communication Technology Department 59
Palompon Institute of Technology
INTRODUCTION TO SQL FOR VISUAL BASIC PROGRAMMING
7
Example 3: To display the Companies in reverse alphabetical order:
Result:
Company OrderNumber
Williams 6798
Williams 2312
Sega 3412
ABC Shop 5678
Syntax:
You can also specify the columns for which you want to insert data:
Result:
LastName FirstName Address City
Pettersen Karl Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes
7
Will give this result:
LastName FirstName Address City
Pettersen Karl Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes
Rasmussen Storgt 67
Syntax:
Result:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Storgt 67
Result:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Stien 12 Stavanger
Syntax:
DELETE FROM table-name WHERE column-name = some-value
7
The “Persons” table:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Stien 12 Stavanger
Result:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
It is also possible to delete all rows in a table without deleting the table. This means that the table
structure, attributes, and indexes will still be intact:
Syntax:
Result:
LastName FirstName Address City
Syntax:
7
Example 1: To count the number of records in the Persons table:
Result:
4
Result:
2