Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL query one to many relationship
    primarykey
    data
    text
    <p>I have a table for Employees and another table with Training. The training table contains various training classes that the employees have completed. We have mandatory security awareness training, so every employee must complete this training class. I’m having trouble running a query that will return ALL employees' either listed completing the training or not.</p> <p>Example Employee table</p> <pre><code>╔════╦══════╗ ║ ID ║ NAME ║ ╠════╬══════╣ ║ 1 ║ Bob ║ ║ 2 ║ Tom ║ ║ 3 ║ John ║ ╚════╩══════╩ </code></pre> <p>Example Training table</p> <pre><code>╔════╦══════════════╦════════════════════╗ ║ ID ║ DEPARTMENT_ID║ CLASS ║ ╠════╬══════════════╬════════════════════╣ ║ 1 ║ 1 ║ Security Awareness ║ ║ 2 ║ 1 ║ Workplace Safety ║ ║ 3 ║ 2 ║ Security Awareness ║ ╚════╩══════════════╩════════════════════╝ </code></pre> <p>Target result</p> <pre><code>╔════╦══════╦════════════════════╗ ║ ID ║ NAME ║ CLASS ║ ╠════╬══════╬════════════════════╣ ║ 1 ║ Bob ║ Security Awareness ║ ║ 2 ║ Tom ║ Security Awareness ║ ║ 3 ║ John ║ (null) ║ ╚════╩══════╩════════════════════╝ </code></pre> <p>The query that I am using is </p> <pre><code>SELECT employee.id, employee.name, training.class FROM employee JOIN training ON employee.id = training.department_id WHERE training.class LIKE '%SECURITY%' ORDER BY employee_id </code></pre> <p>The employee missing the "Security Awareness" class just don't appear, and falls through the cracks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload