Ex.No.
8 TABLE OPERATIONS USING JOIN AND UNION
Statement of problem
create any two tables with common column name and perform join and union
Aim
To create any two tables with common column name and perform join and union
PROCEDURE:
Create Database
CREATE DATABASE college;
show the database
SHOW DATABASES;
Using the database
USE college;
Create the 'students' table
CREATE TABLE students (
student_id INT PRIMARY KEY,
student_name VARCHAR(50),
age INT
);
Desc students;
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50),
student_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id)
);
Desc courses;
Insert some records into the 'students' and ‘ courses’ table
I INSERT INTO students (student_id, student_name, age) VALUES
(1, 'Alice', 20),
(2, 'Bob', 22),
(3, 'Charlie', 23);
INSERT INTO courses (course_id, course_name, student_id) VALUES
(101, 'Math', 1),
(102, 'Science', 2),
(103, 'History', 3),
(104, 'Art', 1);
Selecting the table
select * from students;
select * from courses;
Performing a Inner Join
SELECT students.student_id, students.student_name, courses.course_name
FROM students
INNER JOIN courses ON students.student_id = courses.student_id;
Performing a Union
SELECT student_name AS name FROM students
UNION
SELECT course_name AS name FROM courses;
Result
The mysql query to create any two tables with common column name and perform join and
union has been done successfully.
OUTPUT:
Students table structure:
courses table structure:
Viewing the contents of ‘students’ table:
Viewing the contents of ‘courses’ table:
Performing inner join:
Performing union: