Create and Populate Tables
In this step, you will create two tables, employees
and departments
, and populate them with sample data. These tables will be used in the subsequent steps to demonstrate table joining techniques.
First, open the SQLite shell by running the following command in the terminal:
sqlite3 /home/labex/project/company.db
This command opens the SQLite shell and connects to a database file named company.db
. If the file doesn't exist, SQLite will create it.
Now, create the employees
table with the following SQL command:
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT,
department_id INTEGER
);
This command creates a table named employees
with three columns: id
, name
, and department_id
. The id
column is the primary key and will uniquely identify each employee.
Next, create the departments
table with the following SQL command:
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT
);
This command creates a table named departments
with two columns: id
and name
. The id
column is the primary key and will uniquely identify each department.
Now, insert some sample data into the employees
table:
INSERT INTO employees (name, department_id) VALUES
('Alice', 1),
('Bob', 2),
('Charlie', 1),
('David', 3),
('Eve', NULL);
This command inserts five rows into the employees
table. Each row represents an employee and their corresponding department ID.
Finally, insert sample data into the departments
table:
INSERT INTO departments (name) VALUES
('Sales'),
('Marketing'),
('Engineering');
This command inserts three rows into the departments
table. Each row represents a department and its name.
To verify that the tables have been created and populated correctly, you can run the following SQL command:
SELECT * FROM employees;
Expected Output:
1|Alice|1
2|Bob|2
3|Charlie|1
4|David|3
5|Eve|
And:
SELECT * FROM departments;
Expected Output:
1|Sales
2|Marketing
3|Engineering