My 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
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
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server,
IBM DB2, Oracle, MySQL, and Microsoft Access.
The keywords in SQL are note case sensitive.
The data in RDBMS is stored in database objects called tables. A table is a collection of
related data entries and it consists of columns and rows.
Example:
[Link]
The SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only
want to list the different (distinct) values.
SELECT DISTINCT Syntax
CustomerI CustomerNam ContactNam Address City PostalCod Country
D e e e
1 Alfreds Maria Obere Str. Berlin 12209 German
Futterkiste Anders 57 y
2 Ana Trujillo Ana Trujillo Avda. de la Méxic 05021 Mexico
Emparedados Constitución o D.F.
y helados 2222
3 Antonio Antonio Mataderos Méxic 05023 Mexico
Moreno Moreno 2312 o D.F.
Taquería
4 Around the Thomas 120 Hanover Londo WA1 1DP UK
Horn Hardy Sq. n
5 Berglunds Christina Berguvsväge Luleå S-958 22 Sweden
snabbköp Berglund n8
Examples (click the link to try out). Note: learn the syntax
[Link]
[Link]
[Link]
The DISTINCT clause can also be applied to one or more columns in the select list of the
SELECT statement.
The SQL WHERE Clause
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
Examples
[Link]
Operators in The WHERE Clause
The following operators can be used in the WHERE clause:
Operator Description Example
= Equal Try it
> Greater than Try it
< Less than Try it
>= Greater than or equal Try it
<= Less than or equal Try it
<> Not equal. Note: In some versions of SQL this operator may be Try it
written as !=
BETWEEN Between a certain range Try it
LIKE Search for a pattern Try it
IN To specify multiple possible values for a column Try it
Do the following exercise 1 to exercise 5 by clicking on the link
[Link]
SQL Wildcard Characters
A wildcard character is used to substitute one or more characters in a string.
Wildcard characters are used with the LIKE operator. The LIKE operator is used in
a WHERE clause to search for a specified pattern in a column.
Examples:
[Link]
[Link]
[Link]
[Link]
Wildcard Characters in SQL Server
Symbol Description Example
% Represents zero or more characters bl% finds bl, black, blue, and blob
_ Represents a single character h_t finds hot, hat, and hit
[] Represents any single character within the h[oa]t finds hot and hat, but not
brackets hit
^ Represents any character not in the brackets h[^oa]t finds hit, but not hot and
hat
- Represents a range of characters c[a-b]t finds cat and cbt
The SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
• The percent sign (%) represents zero, one, or multiple characters
• The underscore sign (_) represents one, single character
Examples:
[Link]
[Link]
Try exercise 1 to exercise 5 by clicking on the link below
[Link]
The SQL COUNT(), AVG() and SUM() Functions
The COUNT() function returns the number of rows that matches a specified criterion.
Demo Database
Below is a selection from the "Products" table in the Northwind sample database:
ProductID ProductName SupplierID CategoryID Unit Price
1 Chais 1 1 10 boxes x 20 18
bags
2 Chang 1 1 24 - 12 oz 19
bottles
3 Aniseed Syrup 1 2 12 - 550 ml 10
bottles
4 Chef Anton's Cajun 2 2 48 - 6 oz jars 22
Seasoning
5 Chef Anton's Gumbo Mix 2 2 36 boxes 21.35
Examples
[Link]
[Link]
[Link]
The SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:
• The AND operator displays a record if all the conditions separated by AND are TRUE.
• The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
Examples
[Link]
[Link]
Try exercise 1 to 5
[Link]
The SQL MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
Demo Database
Below is a selection from the "Products" table in the Northwind sample database:
ProductID ProductName SupplierID CategoryID Unit Price
1 Chais 1 1 10 boxes x 20 18
bags
2 Chang 1 1 24 - 12 oz 19
bottles
3 Aniseed Syrup 1 2 12 - 550 ml 10
bottles
4 Chef Anton's Cajun 2 2 48 - 6 oz jars 22
Seasoning
5 Chef Anton's Gumbo Mix 2 2 36 boxes 21.35
Examples:
[Link]
[Link]
practice exercise 1 to 5
[Link]
Learning SQL: INSERT, UPDATE, DELETE Statement
📋 Database Table Used: Customers
This is the table we'll be using for all examples:
CustomerID CustomerName Phone Address City Country
1 Alice Johnson 555-0123 123 Elm Street New York USA
2 Ravi Kumar 982-1234567 7 MG Road Mumbai India
3 Fatima Zahra 212-3344556 42 Pearl St Casablanca Morocco
4 John Smith 777-332211 98 King Ave London UK
5 Mei Lin 010-88997766 55 Lotus Rd Beijing China
INSERT Statement
Purpose:
Used to add a new record (row) into a table.
Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
• Strings (text) must be in single quotes: 'Paris', 'John Doe'
• Numbers do not need quotes: 6, 123456
Example: Add Carlos Mendes to the table
INSERT INTO Customers (CustomerID, CustomerName, Phone, Address, City,
Country)
VALUES (6, 'Carlos Mendes', '351-9123456', '21 Rosa St', 'Lisbon',
'Portugal');
Resulting Table:
CustomerID CustomerName Phone Address City Country
1 Alice Johnson 555-0123 123 Elm Street New York USA
2 Ravi Kumar 982-1234567 7 MG Road Mumbai India
3 Fatima Zahra 212-3344556 42 Pearl St Casablanca Morocco
4 John Smith 777-332211 98 King Ave London UK
5 Mei Lin 010-88997766 55 Lotus Rd Beijing China
6 Carlos Mendes 351-9123456 21 Rosa St Lisbon Portugal
UPDATE Statement
Purpose:
Used to change or modify the data in existing records in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
• Always use a WHERE clause to target the correct row
• Strings must be in single quotes
• ⚠️ Without WHERE, all rows will be updated
🧪 Example: Change Ravi Kumar’s city to Delhi
UPDATE Customers
SET City = 'Delhi'
WHERE CustomerID = 2;
📈 Resulting Table:
CustomerID CustomerName Phone Address City Country
1 Alice Johnson 555-0123 123 Elm Street New York USA
2 Ravi Kumar 982-1234567 7 MG Road Delhi India
3 Fatima Zahra 212-3344556 42 Pearl St Casablanca Morocco
4 John Smith 777-332211 98 King Ave London UK
5 Mei Lin 010-88997766 55 Lotus Rd Beijing China
6 Carlos Mendes 351-9123456 21 Rosa St Lisbon Portugal
DELETE Statement
Purpose: Used to remove one or more records from a table.
Syntax:
DELETE FROM table_name
WHERE condition;
• Use a WHERE clause to select which record(s) to delete
• ⚠️ If you don’t include a WHERE, all rows will be deleted!
Example: Remove John Smith from the table
DELETE FROM Customers
WHERE CustomerID = 4;
📈 Resulting Table:
CustomerID CustomerName Phone Address City Country
1 Alice Johnson 555-0123 123 Elm Street New York USA
2 Ravi Kumar 982-1234567 7 MG Road Delhi India
3 Fatima Zahra 212-3344556 42 Pearl St Casablanca Morocco
5 Mei Lin 010-88997766 55 Lotus Rd Beijing China
6 Carlos Mendes 351-9123456 21 Rosa St Lisbon Portugal
Summary Table: When to Use What
Operation Action Statement Format
INSERT Add a new row INSERT INTO ... VALUES (...);
UPDATE Modify existing data UPDATE ... SET ... WHERE ...;
DELETE Remove one or more rows DELETE FROM ... WHERE ...;