Mysql
Mysql
What is SQL?
RDBMS
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 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:
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
Inside a table, a column often contains many duplicate values; and sometimes you only
want to list the different (distinct) values.
SELECT DISTINCT Syntax
Examples (click the link to try out). Note: learn the syntax
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_distinct2
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_distinct
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_distinct3
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.
Examples
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_where
= Equal Try it
<> Not equal. Note: In some versions of SQL this operator may be Try it
written as !=
https://www.w3schools.com/sql/exercise.asp?filename=exercise_where1
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:
https://www.w3schools.com/sql/exercise.asp?filename=exercise_wildcards1
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_wildcard_percent
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_wildcard_underscore
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_wildcard_charlist&ss=-1
% Represents zero or more characters bl% finds bl, black, blue, and blob
^ Represents any character not in the brackets h[^oa]t finds hit, but not hot and
hat
There are two wildcards often used in conjunction with the LIKE operator:
Examples:
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_like_ending
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_like_underscore
https://www.w3schools.com/sql/exercise.asp?filename=exercise_like1’
Demo Database
Below is a selection from the "Products" table in the Northwind sample database:
2 Chang 1 1 24 - 12 oz 19
bottles
Examples
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_count
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_avg
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_sum
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.
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_where_and
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_where_or2
Try exercise 1 to 5
https://www.w3schools.com/sql/exercise.asp?filename=exercise_where4
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:
1 Chais 1 1 10 boxes x 20 18
bags
2 Chang 1 1 24 - 12 oz 19
bottles
Examples:
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_min
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_max
practice exercise 1 to 5
https://www.w3schools.com/sql/exercise.asp?filename=exercise_functions1
INSERT Statement
Purpose:
Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Resulting Table:
UPDATE Statement
Purpose:
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE Customers
SET City = 'Delhi'
WHERE CustomerID = 2;
📈 Resulting Table:
DELETE Statement
Purpose: Used to remove one or more records from a table.
Syntax:
DELETE FROM table_name
WHERE condition;
📈 Resulting Table: