You are on page 1of 5

Database Programming With Visual Basic 6

Introduction to SQL
SQL is a standard language for accessing and manipulating databases.
What is SQL?
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
• SQL is an ANSI (American National Standards Institute) standard
• Keep in mind SQL is not case sensitive
What Can SQL do?
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert, update and delete records in a database
• SQL can create new databases
• SQL can create new tables in a database
SQL DML and DDL Statements
SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data
Definition Language (DDL).
The query and update commands form the DML part of SQL:
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
The DDL part of SQL permits database tables to be created or deleted. It also define
indexes (keys), specify links between tables, and impose constraints between tables. The
most important DDL statements in SQL are:
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index
The SQL SELECT Statement
The SELECT statement is used to select data from a database.The result is stored in a
result table, called the result-set.
SQL SELECT Syntax
SELECT * or column_name(s) FROM table_name
SQL DISTINCT Clause
The DISTINCT keyword can be used to elimate the duplicate values.
An SQL SELECT Example
1. SELECT * FROM Employee (select all column from table)
2. SELECT Emp_Code,Emp_Name FROM Employee
3. SELECT Distinct Emp_Name FROM Employee

SQL WHERE Clause


The WHERE clause is used to filter records.
Database Programming With Visual Basic 6
SQL WHERE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
Operators Allowed in the WHERE Clause
With the WHERE clause, the following operators can be used:
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
IN If you know the exact value you want to return for at least one of the columns
SQL AND & OR Operators
The AND & OR operators are used to filter records based on more than one condition.
SQL Wildcards
SQL wildcards can substitute for one or more characters when searching for data in a
database.SQL wildcards must be used with the SQL LIKE operator.With SQL, the following
wildcards can be used:
Wildcard Description
% A substitute for zero or more characters
_ A substitute for exactly one character
[charlist] Any single character in charlist
[^charlist] Any single character not in charlist
WHERE Clause Example
• SELECT * FROM Employee WHERE Emp_Code=1001
• SELECT * FROM Employee WHERE Emp_Name=’Ali’
• SELECT * FROM Employee WHERE Salary>=15000
• SELECT * FROM Employee WHERE Salary BETWEEN 15000 and 25000
• SELECT * FROM Employee WHERE Emp_Name LIKE ‘A%’
• SELECT * FROM Employee WHERE Emp_Name LIKE ‘A_’
• SELECT * FROM Employee WHERE Emp_Name LIKE ‘[AGR]%’
• SELECT * FROM Employee WHERE Dept_Code IN(10,30,50)
• SELECT * FROM Employee WHERE First_Name=”Ali’ AND Last_Name=’Ahmed’
• SELECT * FROM Employee WHERE First_Name=”Ali’ OR Last_Name=’Ahmed’
SQL ORDER BY Clause
The ORDER BY keyword is used to sort the result-set.
Database Programming With Visual Basic 6
SQL ORDER BY Syntax
SELECT column_name(s) FROM table_name
ORDER BY column_name(s) ASC|DESC
ORDER BY Example
• SELECT * FROM Employee ORDER BY Dept_Code
• SELECT * FROM Employee ORDER BY salary DESC,Dept_Code
SQL TOP Clause
The TOP clause is used to specify the number of records to return.
SQL Server Syntax
SELECT TOP number|percent column_name(s) FROM table_name
SQL TOP Example
• SELECT TOP 5 FROM Employee
• SELECT TOP 50 PERCENT * FROM Employee
SQL Alias
You can give a table or a column another name by using an alias. This can be a good
thing to do if you have very long or complex table names or column names.
SQL Alias Syntax
SELECT column_name AS alias_name FROM table_name AS alias_name
Alias Example
SELECT e.Emp_Code [Employee Code],e.Emp_Name ‘Employee Name’ FROM Employee e
SQL Joins
SQL joins are used to query data from two or more tables, based on a relationship
between certain columns in these tables.
Different SQL JOINs
Before we continue with examples, we will list the types of JOIN you can use, and the
differences between them.
• INNER JOIN: Return rows when there is at least one match in both tables
• LEFT JOIN: Return all rows from the left table, even if there are no matches in the
right table
• RIGHT JOIN: Return all rows from the right table, even if there are no matches in
the left table
• FULL JOIN: Return rows when there is a match in one of the tables
SQL INNER JOIN Syntax SQL LEFT JOIN Syntax
SELECT column_name(s) SELECT column_name(s)
FROM table_name1 FROM table_name1
INNER JOIN table_name2 LEFT JOIN table_name2
ON table_name1.column_name ON table_name1.column_name
=table_name2.column_name =table_name2.column_name
SQL RIGHT JOIN Syntax SQL FULL JOIN Syntax
SELECT column_name(s) SELECT column_name(s)
FROM table_name1 FROM table_name1
RIGHT JOIN table_name2 FULL JOIN table_name2
ON table_name1.column_name ON table_name1.column_name
=table_name2.column_name =table_name2.column_name
Database Programming With Visual Basic 6
SQL INNER JOIN Example
SELECT * FROM Employee e INNER JOIN Department d ON e.Dept_Code=d.Dept_Code
SQL LEFT JOIN Example
SELECT * FROM Employee e LEFT JOIN Department d ON e.Dept_Code=d.Dept_Code
SQL RIGHT JOIN Example
SELECT * FROM Employee e RIGHT JOIN Department d ON e.Dept_Code=d.Dept_Code
SQL FULL JOIN Example
SELECT * FROM Employee e FULL JOIN Department d ON e.Dept_Code=d.Dept_Code
SQL UNION or UNION ALL Operator
SQL UNION operator combines two or more SELECT statements. The UNION operator
selects only distinct values by default. To allow duplicate values, use UNION ALL. The
column names in the result-set of a UNION are always equal to the column names in the
first SELECT statement in the UNION.
SQL UNION or UNION ALL Syntax
SELECT column_name(s) FROM table_name1
UNION or UNION ALL
SELECT column_name(s) FROM table_name2
SQL UNION Example
SELECT Dept_Code,Dept_Name FROM Department
UNION
SELECT Emp_Code,Emp_Name FROM Employee
SQL NULL Values
NULL values represent missing unknown data.By default, a table column can hold NULL
values.
SQL Functions
SQL has many built-in functions for performing calculations on data.
SQL Aggregate Functions
SQL aggregate functions return a single value, calculated from values in a column.Useful
aggregate functions:
• AVG() - Returns the average value
• COUNT() - Returns the number of rows
• MAX() - Returns the largest value
• MIN() - Returns the smallest value
• SUM() - Returns the sum
SQL Scalar functions
SQL scalar functions return a single value, based on the input value.Useful scalar functions:
• UCASE() - Converts a field to upper case
• LCASE() - Converts a field to lower case
• MID() - Extract characters from a text field
• LEN() - Returns the length of a text field
• ROUND() - Rounds a numeric field to the number of decimals specified
• NOW() - Returns the current system date and time
SQL GROUP BY Statement
The GROUP BY statement is used in conjunction with the aggregate functions to group the
result-set by one or more columns.
Database Programming With Visual Basic 6
SQL GROUP BY Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
SQL GROUP BY Example
SELECT d.Dept_Name,SUM(e.Salary) Salary FROM Employee e INNER JOIN Department d
ON e.Dept_Code=d.Dept_Code GROUP BY d.Dept_Name
SQL HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions.
SQL HAVING Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
SQL HAVING Example
SELECT d.Dept_Name,SUM(e.Salary) Salary FROM Employee e INNER JOIN Department d
ON e.Dept_Code=d.Dept_Code GROUP BY d.Dept_Name HAVING SUM(e.Salary) >15000
SQL INSERT INTO Statement
The INSERT INTO statement is used to insert a new row in a table.
SQL INSERT INTO Syntax
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
SQL INSERT INTO Example
INSERT INTO Department (Dept_Code,Dept_Name) VALUES (60,’Sales’)
SQL UPDATE Statement
The UPDATE statement is used to update records in a table. The WHERE clause specifies
which record or records that should be updated. If you omit the WHERE clause, all records
will be updated!
SQL UPDATE Syntax
UPDATE table_name SET column1=value, column2=value2,...
WHERE some_column=some_value
SQL UPDATE Example
UPDATE Department SET Dept_Name=’Sales and Marketing’ WHERE Dept_Code=60
SQL DELETE Statement
The DELETE statement is used to delete records in a table. The WHERE clause specifies
which record or records that should be deleted. If you omit the WHERE clause, all records
will be deleted
SQL DELETE Syntax
DELETE FROM table_name WHERE some_column=some_value
SQL DELETE Example
DELETE FROM Employee WHERE Dept_Code=30
DELETE FROM Employee (Delete All Records)

You might also like