Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need a couple of self-join tables on employee. One represents the supervisor to employee relation. The second represents the peer relationship between employees.</p> <p>Here is the SQL for PostgreSQL</p> <p>drop schema if exists stackoverflow cascade;</p> <p>create schema stackoverflow;</p> <p>set search_path to stackoverflow, public;</p> <p>create table employee</p> <p>(</p> <pre><code>id serial not null unique, name text not null unique, title text not null, primary key ( id ) </code></pre> <p>);</p> <p>create table reports</p> <p>(</p> <pre><code>supervisorid integer not null references employee ( id ) on delete cascade , subordinateid integer not null references employee ( id ) check ( supervisorid != subordinateid ), unique ( supervisorid, subordinateid ), unique( subordinateid ) </code></pre> <p>);</p> <p>create table peer</p> <p>(</p> <pre><code>supervisorid integer not null references employee ( id ) on delete cascade , peerid integer not null references employee ( id ) check ( supervisorid != peerid ), unique ( supervisorid, peerid ) </code></pre> <p>);</p> <p>create or replace view directreports as</p> <pre><code> select supervisor.id as "supervisor id", supervisor.name as "supervisor name", reporting.id as "employee id", reporting.name as "employee name" from employee supervisor, employee reporting , reports where supervisor.id = reports.supervisorid and reporting.id = reports.subordinateid; create or replace view peerreports as </code></pre> <p>select * from directreports, peer, employee </p> <pre><code> where employee.id = peer.peerid and peer.supervisorid = directreports."supervisor id"; </code></pre> <p>insert into employee (name, title) </p> <pre><code>values ( 'c head', 'c head'), ( 'd head', 'd head'), ('c emp1', 'c emp1' ) , ('c emp2', 'c emp2' ) ; insert into reports select employee.id as "supervisorid", reportsto.id as "subordinateid" from employee, employee reportsto where employee.name = 'c head' and reportsto.name in ('c emp1', 'c emp2' ) and reportsto.name != employee.name ; insert into peer select employee.id as "supervisorid", peerto.id as "peer.peerid" from employee, employee peerto where employee.name = 'c head' and peerto.name = 'd head' and employee.id != peerto.id; </code></pre> <p>Here are the typical queries</p> <p>select * from employee;</p> <p>select * from reports;</p> <p>select * from directreports;</p> <p>select * from peer;</p> <p>select * from peerreports, employee where employee.name= 'd head';</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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