Week 11 - Query
Week 11 - Query
Week 13
Query
Query Editor
What is SQL?
What is SQL?
standard language for storing,
manipulating and retrieving data in
databases.
1. Create database
2. Create table
3. Insert data
4. Select data
5. Update data
6. Delete data
What is Database?
Before we What is Data?
understand
picture, image, file, pdf, etc. can also be
considered data.
about Data?
A database is a systematic collection of
What is data. They support electronic storage
and manipulation of data. Databases
Database? make data management easy.
● Mysql
● SQL Server
● Microsoft Access
● Etc.
But, in this lab class we will use online compiler from this website:
https://paiza.io/projects/mRD9znWDPca3lmz_IGGLxA?language=mysql
Why? Because, this online compiler is cross platform, it means that you can do it in any platform such as:
Windows, IOS, Linux, and even your android smartphone.
Creating a Database
To create a database, we can use keywordCREATE DATABASE
followed by the database name to create a new database.
For example:
This statement will create a database called “testDB”
Creating a Table
Creating a Table
To create a table, we can use keyword CREATE TABLE followed by the name of the table to create
a new table.
After that, we need to define the column along with the datatype of each column. Below is the
syntax to create new table:
There are several data types available for the column, but the
most common are:
● integer = represents number
● varchar(size) = represents string of characters
Example:
Assuming that the database is already created, please create a table called “House” with the
following columns:
Please insert the following data to the table “ House” that you have create previously:
House 1:
● Address = “Osprey”
● BedCount = 3
● Price = 2500
House 2:
● Address = “Chantry”
● BedCount = 2
● Price = 1000
Solution
Practice II
For student with the student id is ODD
Please insert the following data to the table “Animal” that you have create previously:
Animal 1:
● ID = 1
● Name = “Cat”
● Age = 2
Animal 2:
● ID = 2
● Name = “Dog”
● Age = 3
For student with the student id is EVEN
Please insert the following data to the table “Plant” that you have create previously:
Plant 1:
● ID = 1
● Name = “American Marigold”
● Age = 2
Plant 2:
● ID = 2
● Name = “Annual Vinca”
● Age = 3
Select Data from Table
Selecting Data from Table
To select/return data from a database, we can use keyword SELECT followed by the specified column
and the table name. The data returned is stored in a result table, called the result-set. Below is the
syntax to select data from a database:
Here, column1, column2, ... are the field names of the table you want to select data from. If you
want to select all the fields available in the table, use the following syntax:
Example:
Please return all data from all available fields in the table “House” that you have inserted
previously.
Solution
WHERE
The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified
condition.
The above code shows the implementation of WHERE clause.
ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword
.
The above code shows the implementation of ORDER BY keyword.
Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to
make column names more readable. An alias only exists for the duration of that query. An alias is created
with the AS keyword.
The above code shows the implementation of AS keyword.
SQL COUNT(), AVG() and SUM() Functions
The COUNT() function returns the number of rows that matches a specified criterion.
The AVG() function returns the average value of a numeric column.
Note: Be careful when updating records in a table! Notice the WHERE clause in the
UPDATE statement. The WHERE clause specifies which record(s) that should be
updated. If you omit the WHERE clause, all records in the table will be updated!
Example:
From the previous table “House” example, modify the BedCount for the House with the address of
‘Osprey’ to 2.
Solution
Delete Data in Table
Deleting Data in Table
To delete data from a database, we can use the keyword DELETE follows by the table name and the
specifier. Below is the syntax to delete a data from the database.
Note: Be careful when deleting records in a table! Notice the WHERE clause in the
DELETE statement. The WHERE clause specifies which record(s) should be deleted. If
you omit the WHERE clause, all records in the table will be deleted!
Example:
From the previous table “House”, please delete a house where the address is ‘Ospray’.
Solution
Practice III
For student with the student id is ODD
From the previous practice II table, please delete an Animal with the id of 2.
Practice I Screenshot your program code and output depending on your student id last digit
Practice II Screenshot your program code and output depending on your student id last digit
Practice II Screenshot your program code and output depending on your student id last digit
Questions?
References
https://www.guru99.com/introduction-to-database-sql.html
https://www.w3schools.com/sql/sql_create_db.asp
https://www.w3schools.com/sql/sql_create_table.asp
https://www.w3schools.com/sql/sql_select.asp
https://www.w3schools.com/sql/sql_insert.asp
https://www.w3schools.com/sql/sql_update.asp
https://www.w3schools.com/sql/sql_delete.asp