Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3852432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:19:03+00:00 2026-05-19T17:19:03+00:00

Is it possible using just SQL and MySQL to get the OUTPUT below? SAMPLE

  • 0

Is it possible using just SQL and MySQL to get the “OUTPUT” below?

SAMPLE DATA: To better elaborate with an example, lets assume I am trying to load a file containing employee name, the offices they have occupied in the past and their Job title history separated by a tab.

File:

EmployeeName<tab>OfficeHistory<tab>JobLevelHistory
John Smith<tab>501<tab>Engineer
John Smith<tab>601<tab>Senior Engineer
John Smith<tab>701<tab>Manager
Alex Button<tab>601<tab>Senior Assistant
Alex Button<tab>454<tab>Manager

NOTE: The single table database is completely normalized (as much as a single table may be) — and for example, in the case of “John Smith” there is only one John Smith; meaning there are no duplicates that would lead to conflicts in referential integrity.

The MyOffice database schema has the following tables:

Employee (nId, name)
Office (nId, number)
JobTitle (nId, titleName)
Employee2Office (nEmpID, nOfficeId)
Employee2JobTitle (nEmpId, nJobTitleID)

OUTPUT: So in this case. the tables should look like:

Employee
1 John Smith
2 Alex Button

Office
1 501
2 601
3 701
4 454

JobTitle
1 Engineer
2 Senior Engineer
3 Manager
4 Senior Assistant

Employee2Office
1 1
1 2
1 3
2 2
2 4

Employee2JobTitle
1 1
1 2
1 3
2 4
2 3

Here’s the MySQL DDL to create the database and tables:

create database MyOffice2;

use MyOffice2;

CREATE TABLE Employee (
      id MEDIUMINT NOT NULL AUTO_INCREMENT,
      name CHAR(50) NOT NULL,
      PRIMARY KEY (id)
    ) ENGINE=InnoDB;

CREATE TABLE Office (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  office_number INT NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB;

CREATE TABLE JobTitle (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  title CHAR(30) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB;

CREATE TABLE Employee2JobTitle (
  employee_id MEDIUMINT NOT NULL,
  job_title_id MEDIUMINT NOT NULL,
  FOREIGN KEY (employee_id) REFERENCES Employee(id),
  FOREIGN KEY (job_title_id) REFERENCES JobTitle(id),
  PRIMARY KEY (employee_id, job_title_id)
) ENGINE=InnoDB;

CREATE TABLE Employee2Office (
  employee_id MEDIUMINT NOT NULL,
  office_id MEDIUMINT NOT NULL,
  FOREIGN KEY (employee_id) REFERENCES Employee(id),
  FOREIGN KEY (office_id) REFERENCES Office(id),
  PRIMARY KEY (employee_id, office_id)
) ENGINE=InnoDB;
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:19:04+00:00Added an answer on May 19, 2026 at 5:19 pm

    You can use a pass through table, and a trigger for this. Periodically, or from your calling app, delete from this table whenever you’re done with it.

    create table TmpEmp (
    EmployeeName char(50) not null,
    OfficeHistory int null,
    JobLevelHistory char(30) null);
    

    Create a trigger on this table

    delimiter |
    CREATE TRIGGER tg_TmpEmp BEFORE INSERT ON TmpEmp
    FOR EACH ROW
    BEGIN
    IF not exists (select * from Employee where Name = NEW.EmployeeName) THEN
        INSERT INTO Employee(name)
            select NEW.EmployeeName;
    END IF;
    IF not exists (select * from Office where office_number = NEW.OfficeHistory) THEN
        INSERT INTO Office(office_number)
            select NEW.OfficeHistory;
    END IF;
    IF not exists (select * from JobTitle where title = NEW.JobLevelHistory) THEN
        INSERT INTO JobTitle(title)
            select NEW.JobLevelHistory;
    END IF;
    INSERT INTO Employee2JobTitle(employee_id,job_title_id)
        select E.id, T.id
        from Employee E
        inner join JobTitle T on T.title = NEW.JobLevelHistory
        where E.Name = NEW.EmployeeName
            AND not exists (select *
                from Employee2JobTitle J
                where J.employee_id = E.id and J.job_title_id = T.id);
    INSERT INTO Employee2Office(employee_id,office_id)
        select E.id, O.id
        from Employee E
        inner join Office O on O.office_number = NEW.OfficeHistory
        where E.Name = NEW.EmployeeName
            AND not exists (select *
                from Employee2Office J
                where J.employee_id = E.id and J.office_id = O.id);
    END; |
    delimiter ;
    

    Note: The benefit of this trigger and table is that it works whether you are using LOAD-FILE or just plain inserts. The trigger gets fired and adds data where it needs to.

    Test it

    insert tmpEmp(EmployeeName,OfficeHistory,JobLevelHistory)
    select 'John Smith',501,'Engineer' union all
    select 'John Smith',601,'Senior Engineer' union all
    select 'John Smith',701,'Manager' union all
    select 'Alex Button',601,'Senior Assistant' union all
    select 'Alex Button',454,'Manager';
    
    truncate table tmpEmp;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to get data from remote server using JUST TSQL? I mean
Using just an sql query is it possible to write the contents of a
Possible Duplicate: Are dynamic mysql queries with sql escaping just as secure as prepared
Hello guys. I think it isn't possible just using PHP, but just to be
Is it possible to connect two PCs without a router using just an ethernet
Given five random elements, it's possible to find the median using just six comparisons.
is it possible to delete a file(s) inside the directory by just using javascript
I've just started using Git and it's possible I've missed something obvious, but here
using jqPlot, is it possible to limit the point labels to just one series?
Possible Duplicate: SQL exclude a column using SELECT * [except columnA] FROM tableA? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.