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

  • SEARCH
  • Home
  • 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 7695351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:31:41+00:00 2026-05-31T21:31:41+00:00

I have a problem making an advanced (for me) query. I am trying to

  • 0

I have a problem making an advanced (for me) query.

I am trying to get the first name, last name, title, department name, and first and last name of the manager. I am using this SQL in PHP if for some reason you needed to know. I limit to 10 because the database is enormous. I need only the entry for the employees latest data. They have more than one entry because of job promotions, etc.

I included everything I think you would need to understand my problem. Thanks if you can help.

mysql>
SELECT DISTINCT employees.first_name, employees.last_name,
titles.title, departments.dept_name, z.first, z.last
FROM employees, dept_emp, departments, titles, 
(
  SELECT employees.first_name AS first, employees.last_name AS last
  FROM employees, dept_emp, dept_manager 
  WHERE 
        employees.emp_no = dept_emp.emp_no 
        AND dept_manager.emp_no = dept_emp.emp_no
) AS z
WHERE
   employees.emp_no = dept_emp.emp_no 
   AND dept_emp.dept_no = departments.dept_no
   AND titles.emp_no = employees.emp_no
LIMIT 10;

+------------+-----------+-----------------+-------------+-----------+--------------+
| first_name | last_name | title           | dept_name   | first     | last         |
+------------+-----------+-----------------+-------------+-----------+--------------+
| Georgi     | Facello   | Senior Engineer | Development | Margareta | Markovitch   |
| Georgi     | Facello   | Senior Engineer | Development | Vishwani  | Minakawa     |
| Georgi     | Facello   | Senior Engineer | Development | Ebru      | Alpin        |
| Georgi     | Facello   | Senior Engineer | Development | Isamu     | Legleitner   |
| Georgi     | Facello   | Senior Engineer | Development | Shirish   | Ossenbruggen |
| Georgi     | Facello   | Senior Engineer | Development | Karsten   | Sigstam      |
| Georgi     | Facello   | Senior Engineer | Development | Krassimir | Wegerle      |
| Georgi     | Facello   | Senior Engineer | Development | Rosine    | Cools        |
| Georgi     | Facello   | Senior Engineer | Development | Shem      | Kieras       |
| Georgi     | Facello   | Senior Engineer | Development | Oscar     | Ghazalie     |
+------------+-----------+-----------------+-------------+-----------+--------------+
10 rows in set (0.00 sec)

The database is the employees database from mysql.com:
http://dev.mysql.com/doc/index-other.html

mysql> desc departments;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dept_no   | char(4)     | NO   | PRI | NULL    |       |
| dept_name | varchar(40) | NO   | UNI | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.22 sec)

mysql> desc dept_emp;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| emp_no    | int(11) | NO   | PRI | NULL    |       |
| dept_no   | char(4) | NO   | PRI | NULL    |       |
| from_date | date    | NO   |     | NULL    |       |
| to_date   | date    | NO   |     | NULL    |       |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.20 sec)

mysql> desc dept_manager;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| dept_no   | char(4) | NO   | PRI | NULL    |       |
| emp_no    | int(11) | NO   | PRI | NULL    |       |
| from_date | date    | NO   |     | NULL    |       |
| to_date   | date    | NO   |     | NULL    |       |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.21 sec)

mysql> desc employees;
+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no     | int(11)       | NO   | PRI | NULL    |       |
| birth_date | date          | NO   |     | NULL    |       |
| first_name | varchar(14)   | NO   |     | NULL    |       |
| last_name  | varchar(16)   | NO   |     | NULL    |       |
| gender     | enum('M','F') | NO   |     | NULL    |       |
| hire_date  | date          | NO   |     | NULL    |       |
+------------+---------------+------+-----+---------+-------+
6 rows in set (0.32 sec)

mysql> desc salaries;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| emp_no    | int(11) | NO   | PRI | NULL    |       |
| salary    | int(11) | NO   |     | NULL    |       |
| from_date | date    | NO   | PRI | NULL    |       |
| to_date   | date    | NO   |     | NULL    |       |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.34 sec)

mysql> desc titles;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| emp_no    | int(11)     | NO   | PRI | NULL    |       |
| title     | varchar(50) | NO   | PRI | NULL    |       |
| from_date | date        | NO   | PRI | NULL    |       |
| to_date   | date        | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.60 sec)
  • 1 1 Answer
  • 0 Views
  • 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-31T21:31:43+00:00Added an answer on May 31, 2026 at 9:31 pm

    I am trying to get the first name, last name, title, department name, and first and last name of the manager

    Using the theory (but it’s probably not optimized if you have large tables), do it step by step. Assuming you have a $date PHP variable that contains a date (like the result from date('Y-m-d 00:00:00')), or otherwise you may use MySQL’s date.

    1/ get employees’ identity:

    SELECT
        e.first_name,
        e.last_name
    FROM
        employees AS e
    WHERE
        1
    

    2/ add his/her current title

    SELECT
        e.first_name,
        e.last_name,
        t.title
    FROM
        employees AS e,
        titles AS t
    WHERE
        e.emp_no=t.emp_no AND t.from_date<='$date' AND t.to_date>='$date'
    

    3/ get his/her current department (from dept_emp) and retrieve the department’s name

    SELECT
        e.first_name,
        e.last_name, t.title,
        d.dept_name
    FROM
        employees AS e,
        titles AS t,
        dept_emp AS de,
        departments as d
    WHERE
        e.emp_no=t.emp_no AND t.from_date<='$date' AND t.to_date>='$date'
    AND
        e.emp_no=de.emp_no AND de.from_date<='$date' AND de.to_date>='$date'
    AND
        d.dept_no=de.dept_no
    

    4/ get the department’s manager (from dept_manager) and retrieve his/her name (from employees)

    SELECT
        e.first_name AS empFN,
        e.last_name AS empLN,
        t.title AS empT,
        d.dept_name AS dept,
        em.first_name AS manFN,
        em.last_name AS manLN
    FROM
        employees AS e,
        titles AS t,
        dept_emp AS de,
        departments as d,
        dept_manager AS dm,
        employees AS em
    WHERE
        e.emp_no=t.emp_no AND t.from_date<='$date' AND t.to_date>='$date'
    AND
        e.emp_no=de.emp_no AND de.from_date<='$date' AND de.to_date>='$date'
    AND
        d.dept_no=de.dept_no
    AND
        em.emp_no=dm.emp_no AND dm.from_date<='$date' AND dm.to_date>='$date'
    AND
        de.dept_no=dm.dept_no
    

    There you can append a LIMIT (and an ORDER BY probably, you don’t know which results will be picked in case of a limit), and/or add in the WHERE an emp_no for the employee(s) you want to have info about. Not tested though, but it’s mainly to let you understand how to build the request.

    This is theory, and this request is probably be as heavy to process as the one you wrote. Since you use SQL with PHP, it may be better to retrieve only the primary keys you’re interested in, then send short requests to retrieve the information you want. Something like that I mean (using fake functions but the spirit’s here):

    $dateString="from_date<='$date' AND to_date>='$date'";
    $qe=query("SELECT emp_no, first_name, last_name, dept_no FROM employees, dept_emp WHERE dept_emp.emp_no=employees.emp_no LIMIT 10 ORDER BY emp_no DESC");
    while($r=fetch($qe)) {
      $qt=fetch(query("SELECT title FROM titles WHERE emp_no=".$qe['emp_no']." AND ".$dateString));
      $qd=fetch(query("SELECT dept_name FROM departments WHERE dept_no=".$qe['dept_no']." AND ".$dateString));
      $qm=fetch(query("SELECT first_name, last_name FROM employees AS e, dept_manager AS dm WHERE dept_no=".$qe['dept_no']." AND e.emp_no=dm.emp_no AND ".$dateString));
      //echo/process here
    }
    

    Hope this helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a small problem making a query in MySQL. I have the following
This problem is making me a bit crazy. I have something like this <h3>Feeds
many people seem to have problem in making this work, so i am not
My only problem is making them line up three-across and have equal spacing. Apparently,
I am making an application with Java Swing and i have a problem. I
I have a very big problem. I am making a CRM (Costumer Relationship Management)
I have a tiny (rikiki) problem in SWT ... I am making a small
Here is my problem. I have created a pretty heavy readonly class making many
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
i have a problem. i m making Some wpf button Dynamic through C#.i m

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.