Download presentation
Presentation is loading. Please wait.
1
CHAPTER 7 DATABASE ACCESS THROUGH WEB
PART 2 OF 2 [Structured Query Language] Madam Hazwani binti Rahmat
2
WHAT IS SQL? SQL stands for Structured Query Language.
It is pronounced as “SQL” or “sequel. SQL is a query language used for accessing and modifying information in the database. Although SQL is an ANSI (American National Standards Institute) standard, there are many different versions of the SQL language. However, to be compliant with the ANSI standard, all SQL versions support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
3
SQL STATEMENTS SQL is not case sensitive.
It is a simple language with a limited number of commands and those commands are very readable and are almost structured like English sentences. Semicolon is the standard way to separate each SQL command in database systems that allow more than one SQL statement to be executed in the same call to the server. Some database systems require a semicolon at the end of each SQL statement. MS Access and SQL Server 2000 and do not require a semicolon after each SQL statement, but some database programs force it.
4
SQL DML AND DDL SQL can be divided into TWO parts:
Data Manipulation Language (DML) DML refers to the query and update commands. It is used to insert, select, update, and delete records in the database. Basic DML commands are: SELECT - Retrieves data from the database INSERT - Inserts new data into the database UPDATE - Updates existing data in the database DELETE - Deletes existing data from the database
5
SQL DML AND DDL Data Definition Language (DDL)
The DDL part of SQL permits database tables to be created or deleted. 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
6
SQL SELECT STATEMENT The SELECT statement is used to select data from a database. The SELECT statement returned ALL the results from the queried database table. A simple SQL SELECT Statement, requires : Column(s) Name The columns that will be returned in the results. Table Name. The table that will be queried to retrieve the desired results.
7
SQL SELECT STATEMENT Syntax (To select one / more columns) :
SELECT column_name FROM table_name Example : SELECT firstName, age FROM employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 firstName age Alex 28 Billy 40 Craig 35 Original Data : Table employee Query Result
8
SQL SELECT STATEMENT Syntax (To select ALL columns) :
SELECT * FROM table_name Example : SELECT * FROM employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : Table employee Query Result
9
SQL WHERE CLAUSE The WHERE clause not only used in SELECT statement, but it is also used in UPDATE, DELETE statement etc. The WHERE clause is used to FILTER records that satisfy a given condition. The WHERE clause returned MATCHING results from the queried database table restricted to a specific condition. “condition” is the filter to be applied on the results. The filter could be a range, single value or sub query.
10
SQL WHERE CLAUSE Syntax (applicable for one / more columns) :
SELECT column_name FROM table_name WHERE column_name = ‘criteria’ SQL uses single quotes around text values (most database systems will also accept double quotes). However, numeric values should not be enclosed in quotes.
11
SQL WHERE CLAUSE Example (for text values):
SELECT firstName,lastName FROM employee WHERE lastName = ‘James’ empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 firstName lastName Alex James Billy Original Data : Table employee Query Result
12
SQL WHERE CLAUSE Example (for numeric values):
SELECT firstName,lastName FROM employee WHERE age= 28 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 firstName lastName Alex James Original Data : Table employee Query Result
13
SQL AND , OR & NOT The WHERE clause can filter records based on more than one condition using operators. With the WHERE clause, the following operators can be used: Operator Description = Equal <> or != Not equal > Greater than < Less than >= Greater than or equal LIKE Search for a pattern
14
SQL AND , OR & NOT The AND operator can be used to join two or more conditions in the WHERE clause. Both sides of the AND condition must be true in order for the condition to be met and for those rows to be displayed. Syntax (applicable for one / more columns) : SELECT column_name FROM table_name WHERE condition1 AND condition_N both conditions must be TRUE
15
SQL AND , OR & NOT Example (applicable for one / more columns) :
SELECT empID, firstName, age FROM employee WHERE lastName = ‘James’ AND age > 20 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName age 1 Alex 28 2 Billy 40 Original Data : Table employee Query Result
16
SQL AND , OR & NOT The OR operator can be used to join TWO or more conditions in the WHERE clause also. However, either or both side of the OR operator can be true and the condition will be met - hence, the rows will be displayed. Syntax (applicable for one / more columns) : SELECT column_name FROM table_name WHERE condition1 OR condition_N either or both conditions can be TRUE
17
SQL AND , OR & NOT Example (applicable for one / more columns) :
SELECT empID, firstName, age FROM employee WHERE firstName = ‘Alex’ OR lastName = ‘Dave’ empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName age 1 Alex 28 3 Craig 35 Original Data : Table employee Query Result
18
SQL AND , OR & NOT You can also combine AND and OR (use parenthesis to form complex expressions). Syntax (applicable for one / more columns) : SELECT column_name FROM table_name WHERE condition1 AND (condition_X OR condition_Y )
19
SQL AND , OR & NOT Example (applicable for one / more columns) :
SELECT empID, firstName, age FROM employee WHERE age < 40 AND (firstName = ‘Alex’ OR lastName = ‘James’ ) empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName age 1 Alex 28 Original Data : Table employee Query Result
20
SQL AND , OR & NOT To find rows that do not satisfy a condition, the logical operator, NOT can be used. If a condition is satisfied, then the row is not returned. Syntax (applicable for one / more columns) : SELECT column_name FROM table_name WHERE NOT condition
21
SQL AND , OR & NOT Example (applicable for one / more columns) :
SELECT empID, firstName, age FROM employee WHERE NOT lastName = ‘James’ empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName age 3 Craig 35 Original Data : Table employee Query Result
22
SQL LIKE The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. The LIKE pattern matching operator can also be used in the conditional selection of the WHERE clause. LIKE is a very powerful operator that allows to select only rows that are "like" what specified. The percent sign "%" can be used as a wildcard to match any possible character that might appear before or after the characters specified.
23
SQL LIKE Syntax(applicable for one / more columns) :
SELECT column_name FROM table_name WHERE column_name LIKE pattern Example(applicable for one / more columns) : SELECT empID, lastName, age FROM employee WHERE lastName LIKE ‘%a%’ empID lastName age 1 James 28 2 40 3 Dave 35 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Query Result Original Data : Table employee
24
SQL DISTINCT In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes there is a need to list only the different (distinct) values in a table. The DISTINCT keyword can be used to return only distinct (different) values. The SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the duplicate records and fetching only unique records.
25
SQL DISTINCT Syntax: Example: Query Result
SELECT DISTINCT column_name FROM table_name Example: SELECT DISTINCT lastName FROM employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 lastName James Query Result Original Data : Table employee
26
SQL JOIN The JOIN keyword is used in an SQL statement to query data from TWO or more tables, based on a relationship between certain columns in these tables. Tables in a database are often related to each other with keys.
27
SQL JOIN Syntax(applicable for one / more columns) :
SELECT column_name FROM table_name1 INNER JOIN table_name2 ON table_name1.PK = table_name2.FK The INNER JOIN keyword returns rows when there is at least one match in both tables. If there are rows in table1 that do not have matches in table 2, those rows will NOT be listed.
28
SQL JOIN Example (applicable for one / more columns) : Query Result
SELECT firstname, department.division FROM employee INNER JOIN department ON employee.empID = department.empID firstName division empID Alex Sales 1 Billy Marketing 2 Craig HR 3 Query Result empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 deptID division empID 1 HR 3 2 Sales Marketing Original Data : Table1 : employee Original Data : Table2 : department
29
SQL ORDER BY The ORDER BY keyword is used to sort the query result by a specified column. Syntax(sort by one column) : SELECT * FROM table_name ORDER BY column_name Example (sort by one column) : SELECT * FROM employee ORDER BY firstName empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Query Result
30
SQL ORDER BY Syntax(sort by multiple column) :
SELECT * FROM table_name ORDER BY column_name1, column_name_n Example (sort by multiple column) : SELECT * FROM employee ORDER BY firstName, lastName empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : employee empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Query Result
31
SQL ORDER BY DESC The ORDER BY keyword sorts the records in ascending order by default. Syntax(sort by one column) : SELECT * FROM table_name ORDER BY column_name1 DESC Example (sort by one column) : SELECT * FROM employee ORDER BY age empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : employee empID firstName lastName age 2 Billy James 40 3 Craig Dave 35 1 Alex 28 Query Result
32
SQL INSERT The INSERT INTO statement is used to insert a new record in a table. Syntax(all column) : INSERT INTO table_name VALUES (value1, value2, value_n) Syntax(certain column) : INSERT INTO table_name (column1, column2, column_n) When adding a new row, ensure the data type of the value and the column matches and follow the integrity constraints, if any, defined for the table.
33
SQL INSERT Example(all column) : Original Data : employee Query Result
INSERT INTO employee VALUES (4, ‘Matt’, ‘Kent’, 30) empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 Original Data : employee Query Result
34
SQL INSERT Example(all column) : Original Data : employee Query Result
INSERT INTO employee (empID, firstName, lastName) VALUES (5, ‘Henry’, ‘Owen’) empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 5 Henry Owen Original Data : employee Query Result
35
SQL UPDATE The UPDATE statement is used to update existing records in a table. Syntax(all column) : UPDATE table_name SET column1=value1, column1=value1, column_n=value_n WHERE columnX=valueX The WHERE clause specifies which record or records that should be updated. If it is omitted, all records will be updated.
36
SQL UPDATE Example(all column) : Query Result Original Data : employee
UPDATE employee SET age=29 WHERE empID=5 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 5 Henry Owen empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 5 Henry Owen 29 Query Result Original Data : employee
37
SQL DELETE The DELETE statement is used to delete records in a table.
Syntax(one record) : DELETE FROM table_name WHERE columnX=valueX 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: Syntax(all records) : DELETE FROM table_name or DELETE * FROM table_name
38
SQL DELETE Example(all column) : Original Data : employee Query Result
DELETE FROM employee WHERE empID=4 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 4 Matt Kent 30 empID firstName lastName age 1 Alex James 28 2 Billy 40 3 Craig Dave 35 Original Data : employee Query Result
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.