บันทึกการใช้งาน SQL INTRO สำหรับนักวิทยาศาสตร์ข้อมูล Data Science - sprint 02
ในบทความทั้ง สามบทในการเรียนรู้นี้จะใช้ SQL Online ตาม Link ด้านล่างนี้เป็นเครื่องมือในการทดสอบ Script Code สามารถ Copy Code ไปทดสอบและทำความเข้าใจได้เลยครับ
SQL Online Tool : sqliteonline
Create Table 01
CREATE TABLE employee (
id INT UNIQUE,
name TEXT,
department TEXT,
position TEXT,
salary REAL
);
Insert Data 02
INSERT INTO employee VALUES
(1, 'Devid','Marketing','CEO',10000),
(2,'John','Marketing','VP',85000),
(3,'Marry','Sales','Manager',5999);
Select Data 03 - 04
SELECT * from employee ;
SELECT id, name, salary from employee ;
SELECT id, name, salary from employee LIMIT 1;
Transform Data 05
SELECT
name,
salary,
salary * 1.15 AS new_salary
from employee ;
SELECT
name,
salary,
salary * 1.15 AS new_salary,
UPPER(name) || '@company.com' As company_email
from employee ;
เป็นการสร้าง column ชั่วคราวจะไม่มีผลต่อ table เดิม
- AS คือการตั้งชื่อให้กับ column ใหม่
- LOWER(name) เป็นฟังก์ชัน ตัวพิมพ์เล็ก
- UPPER(name) เป็นฟังก์ชัน ตัวพิมพ์ใหญ่
- || (pipe) เป็นตัว concat
Filter Data 06
SELECT * from employee
WHERE department = "Marketing" and salary > 70000;
SELECT * from employee
WHERE department = 'Marketing' OR department = 'IT';
เทียบได้กับ OR operator
SELECT * from employee
WHERE department in ('Marketing', 'IT');
สร้างเงื่อนไข
SELECT * FROM employee
WHERE salary <= 60000;
Update Data 07
UPDATE employee
set salary = 98000
WHERE id = 1;
SELECT * FROM employee;
Delete Data 08
DELETE from employee
WHERe name = 'Marry';
SELECT * from employee;
DELETE from employee
WHERe id in (2,4);
SELECT * from employee;
Alter Table 09
เป็น comment ใน SQL
alter rename เปลี่ยนชื่อ table
ALTER TABLE employee RENAME to MyEmployee;
SELECT * from MyEmployee;
alter เพิ่ม column
alter table MyEmployee
ADD email TEXT;
SELECT * from MyEmployee;
Copy and Drop Table 10
Copy Table
CREATE TABLE MyEmployee_backup AS
SELECT * from MyEmployee;
Drop Table
DROP TABLE MyEmployee_backup;
คอร์สนี้ดีมากกกก (ไก่ ล้านตัว) ใครอ่านจบ แนะนำว่าให้ไปสมัครเรียน ติดตามได้ที่ link ด้านล่างนี้เลย
Course Online DATA ROCKIE Bootcamp