This task presents SQL basic commands which I used to create a new table with its data. I queried the sales data of furniture, office supplies, and technology.
3. Database and Database
Management System
A database is an organized
collection of data. Example:
bank, library
Database Management System
(DBMS) is a computer software that is
designed to manage, store, and
manipulate data.
Examples: MySQL, PostgreSQL,
Oracle
SQL (Structured Query
Language)
SQL is a language used in
programming to interact with the
database
SQL is the standard language that is
used in relational database
management system (RDMS)
4. SQL
Commands
DDL
Data Definition
Language
Change the table
structure which
includes creating
tables, deleting tables,
adding columns
Example:
- CREATE
- ALTER
- DROP
- TRUNCATE
DML
Data Manipulation
Language
Create changes on the
database
Example:
- INSERT
- UPDATE
- DELETE
DCL
Data Control
Language
Grant or revoke
authority over the
database
Example:
- GRANT
- REVOKE
TCL
Transaction Control
Language
Controlling changes
that are carried out by
DML
Example:
- COMMIT
- ROLLBACK
- SAVEPOINT
DQL
Data Query Language
Query data from the
database
Example:
- SELECT
5. PostgreSQL
Open-source RDBMS that
emphasizes extensibility and
SQL compliance
Types of Data in PostgreSQL
• INT: whole numbers
• FLOAT: positive or negative
number
• SERIAL: pseudo-type integer
• BOOLEAN: True/False
• DATE
• CHAR(n): Character, fixed-length,
blank-padded
• VARCHAR(n): Character, variable-
length
6. Examples of Queries
SELECT NamaKonsumen, Alamat FROM TabelKonsumen WHERE Deposit = 50000
Create a Database and Table
CREATE DATABASE nama_database;
DROP DATABASE nama_database;
Delete a Database and Table
7. Create a Database and Table
CREATE TABLE nama_tabel(
column1 datatype,
column2 datatype,
column3 datatype,
…);
CREATE TABLE karyawan(
id_karyawan SERIAL,
nama VARCHAR(32),
departemen VARCHAR(16),
posisi VARCHAR(16),
…);
Follow the
ordered
column
Insert the data into the table by using INSERT INTO
INSERT INTO
nama_tabel
VALUES
(value1, value2, value3, …);
INSERT INTO
nama_tabel (column1, column2,
column3,...)
VALUES
(value1, value2, value3, …);
Example
8. Delete the Data
DELETE FROM
nama_tabel
WHERE
kondisi;
Deleting data from the table with
a specific condition
DELETE FROM nama_tabel
Deleting all data from the table
Row or other data that
match the condition
will be deleted
9. ALTER TABLE - ADD COLUMN - DROP COLUMN - ALTER COLUMN
Add columns into the table
ALTER TABLE
nama_tabel
ADD COLUMN newcolumn1 datatype,
ADD COLUMN newcolumn2 datatype,
ADD COLUMN newcolumn3 datatype
…
ALTER TABLE
nama_tabel
DROP COLUMN newcolumn1,
DROP COLUMN newcolumn2,
DROP COLUMN newcolumn3
…
Delete columns in the table
ALTER TABLE nama_tabel ALTER COLUMN column_name TYPE
datatype
Change the data type in the column
10.
11. You are hired as a data analyst by a small online store named Tokopaedi which is owned by Mr.
Edi. Tokopaedi does not have a database system and it is your job to build it. Your main tasks
as a data analyst are:
1. Create a database named Tokopaedi in PostgreSQL
2. Create a table named orders with its columns (you need to determine the data type of each
column based on their input)
The column’s header is marked with green
13. Create the Table
Remember the query:
CREATE TABLE_name of table
(column1 datatype, column2
datatype, column3 datatype,…)
And then click Run
14. Insert the Data
Query:
INSERT INTO
orders
VALUES ('CA-
2019-152156','CG-
12520',42420,'FUR-
BO-
10001798',261.96,2,
0,41.9136,'Furniture'
,'Bookcases','Bush
Somerset
Collection
Bookcase','11/8/201
9','11/11/2019','Seco
nd Class','Claire
Gute','Consumer','U
nited
States','Henderson',
'Kentucky','South’)
And then click Run
15. Don’t forget to write SELECT *
FROM orders. It is a query to
present the table
And then click Run