Sunday, May 17, 2015

Write a SQL Program to generate the following output?

Input:
Employee:
Department:
Output:
Creating tables and inserting Data:
CREATE TABLE Employee (
e_id INT NOT NULL AUTO_INCREMENT,
e_name VARCHAR(100) NOT NULL,
age tinyint NOT NULL,
PRIMARY KEY (e_id)
);

CREATE TABLE Department (
d_id INT NOT NULL AUTO_INCREMENT,
d_name VARCHAR(100) NOT NULL,
e_id INT NOT NULL,
PRIMARY KEY (d_id),
FOREIGN KEY (e_id)
REFERENCES Employee(e_id)
ON DELETE CASCADE
);

INSERT INTO Employee VALUES (1,'Ranga', 27), (2, 'Raja', 50), (3, 'Vasu',45) ,
(4, 'Vinod', 27), (5, 'Manoj',27);

INSERT INTO Department VALUES (1,'HR', 2), (2, 'Finance', 4), (3, 'Software',1) ,
(4, 'Finance', 3), (5, 'Hardware',1), (6, 'Software', 5), (7, 'Finance', 1);
Query: 
SELECT group_concat(e.e_name) as Employee_Names, d.d_name as Department_Name FROM Employee e INNER JOIN Department d ON d.e_id = e.e_id GROUP BY d.d_name;
Happy Coding!!!

0 comments: