DEV Community

Clever Cottonmouth
Clever Cottonmouth

Posted on

JOIN 2 TABLE IN SQL

`
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT NOT NULL,
dept_id INT NOT NULL,
salary DECIMAL(10, 2) NOT NULL,
start_date DATE NOT NULL
);

INSERT INTO employees (name, age, dept_id, salary, start_date)
VALUES
('John Doe', 32, 1, 50000, '2022-01-01'),
('Jane Doe', 28, 2, 55000, '2022-02-01'),
('Jim Smith', 35, 3, 60000, '2022-03-01');

SELECT * from employees;

CREATE TABLE departments (
dept_id INT AUTO_INCREMENT PRIMARY KEY,
dept_name VARCHAR(100) NOT NULL,
manager_id INT NOT NULL,
location VARCHAR(100) NOT NULL
);

INSERT INTO departments (dept_name, manager_id, location)
VALUES
('Sales', 1001, 'New York'),
('Marketing', 1002, 'London'),
('Engineering', 1003, 'Berlin');

SELECT * FROM departments;

SELECT employees.name, employees.age, employees.salary, employees.start_date,
departments.dept_name, departments.manager_id, departments.location
FROM employees JOIN departments ON employees.dept_id = departments.dept_id;

`

Top comments (0)