0% found this document useful (0 votes)
2 views57 pages

Week 11 - Query

The document outlines a lab module focused on SQL and Big Data, detailing the creation and management of databases and tables using SQL commands. It includes instructions for inserting, selecting, updating, and deleting data, along with practical exercises for students. Additionally, it emphasizes the use of Power BI's Query Editor for data transformation before loading into the platform.

Uploaded by

zmuqcollege
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views57 pages

Week 11 - Query

The document outlines a lab module focused on SQL and Big Data, detailing the creation and management of databases and tables using SQL commands. It includes instructions for inserting, selecting, updating, and deleting data, along with practical exercises for students. Additionally, it emphasizes the use of Power BI's Query Editor for data transformation before loading into the platform.

Uploaded by

zmuqcollege
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

LAB MODULE

Coding and Big Data

Week 13
Query
Query Editor
What is SQL?

Outline Creating a Database


Creating a Table
Insert Data to Table
Select Data from Table
Update Data in Table
Delete Data in Table
Submission
Big Data is not only about
presenting and visualizing, it is also
about obtaining and accessing the

Big Data data.

Sometimes, we need to obtain a very big


data that is not located in excel file or csv file
because the data is too big. Therefore, we
will see how to access the data in PowerBI
with Query editor as well as from the
database with SQL.
Query Editor
What is Query
Editor in PowerBI? The Query Editor in Power BI is used to
transform or edit data files before they
are actually loaded into the Power BI.
The Query Editor plays the role of an
intermediate data container where you
can modify data by selecting rows and
columns, splitting rows and columns,
pivoting and unpivoting columns, etc.
Interface of The Data Model
Interface of The Query Editor
SQL
SQL (Structured Query Language) is a

What is SQL?
standard language for storing,
manipulating and retrieving data in
databases.

There are 6 basics operations for


the database:

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?

learn about a In simple words, data can be facts related


database, we to any object in consideration. For
example, your name, age, height, weight,
need to etc. are some data related to you. A

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.

Let us also consider Facebook. It needs


to store, manipulate, and present data
related to members, their friends,
member activities, messages,
advertisements, and a lot more.

SQL usually used for storing,


manipulating, and retrieving data in
database.
Creating a Database
Before we start
You can use any dbms (Database Management System) software such as:

● 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:

● Address , with the datatype of string with the size of 90 characters


● BedCount, with the datatype of integer
● Price, with the datatype of integer
Solution
Practice I
For student with the student id is ODD
Assuming that the database is already created, please create a table called “Animal” with the
following columns:

● ID, with the datatype of integer


● Name, with the datatype of string with the size of 90 characters
● Age, with the datatype of integer
For student with the student id is EVEN
Assuming that the database is already created, please create a table called “Plant” with the
following columns:

● ID, with the datatype of integer


● Name, with the datatype of string with the size of 90 characters
● Age, with the datatype of integer
Insert Data to Table
Inserting Data Into Table
To insert data into the table, we can use keyword INSERT INTO follows by the name of the table.
After that, we specify the parameters to define which column that the data belongs to. Below is the
syntax to insert data into the database:
Example:

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.

The SUM() function returns the total sum of a numeric column.


The above code shows the implementation of COUNT function.
The above code shows the implementation of AVG function.
The above code shows the implementation of SUM function.
Update Data in Table
Updating Data in Table
To update data from the database, we can use UPDATE keyword. Below is the syntax to update data from
database:

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.

For student with the student id is EVEN


From the previous practice II table, please delete a Plant with the id of 1.
Submission
Screenshot & Upload to eCampus
Screenshot your codes and terminals, put them into pdf file. Don’t forget to put your name and student id
in the top of the file. Upload the file to e-Campus on Practice Week 9 before Friday.

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

You might also like