Lab schema (HR schema) is listed as below (primary keys are underlined):

Regions(region_id, region_name);
Countries(country_id, country_name, region_id);
Locations(location_id, street_address, postal_code, city, state_province, country_id);
Departments(department_id, department_name, manager_id, location_id);
Jobs(job_id, job_title, min_salary, max_salary);
Employees(employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id);
Job_History(employee_id, start_date, end_date, job_id, department_id);

Learning Objectives:

Write equivalent SQL, Relational Algebra and Datalog query for each of the following questions.

Note that (Rou_T(*) Relation) means to rename the table Relation to T
  and keep the names of all the columns in the original table Relation.

set your session to use HR schema, so that we don't prefix each table with hr.
  1. For some of the employees, they do not belong to any department (or we do not know in which department they work). For each such employee, list his/her full name.
    select first_name, last_name
    from   employees
    where  department_id is null;
    
    Pi_{first_name, last_name} (Sigma_(department_id is null) employees)
    
    Result(fn, ln) ::= Employees(_, fn, ln, _, _, _, _, _, _, _, did)
                    and (did is null)
    
  2. List the name of each employee who once had the title of "Public Accountant".
    select first_name, last_name
    from   employees
    where  employee_id IN (select employee_id
                           from   job_history natural join jobs
                           where  job_title = 'Public Accountant');
    
    Pi_{first_name, last_name} ((Rou_E(*) Employees) Join_(E.employee_id = JH.employee_id)
                               (Rou_JH(*) Job_History) Join_(JH.job_id = Jobs.job_id)
                               (Sigma_(job_title = 'Public Accountant') Jobs))
    
    Result(fn, ln) ::= Employees(eid, fn, ln, _, _, _, _, _, _, _, _)
                   and Job_History(eid, _, _, jid, _)
                   and Jobs(jid, 'Public Accountant', _, _)
    
    
  3. List the name of each employee who once had a title that's not "Public Accountant".
    select first_name, last_name
    from   employees
    where  employee_id IN (select employee_id
                           from   job_history natural join jobs
                           where  job_title <> 'Public Accountant');
    
    Pi_{first_name, last_name} ((Rou_E(*) Employees) Join_(E.employee_id = JH.employee_id)
                               (Rou_JH(*) Job_History) Join_(JH.job_id = Jobs.job_id)
                               (Sigma_(job_title <> 'Public Accountant') Jobs))
    
    Result(fn, ln) ::= Employees(eid, fn, ln, _, _, _, _, _, _, _, _)
                   and Job_History(eid, _, _, jid, _)
                   and Jobs(jid, title, _, _)
                   and title <> 'Public Accountant'
    
  4. List the name of each employee who never had the title of "Public Accountant".
    select first_name, last_name
    from   employees
    where  employee_id NOT IN (select employee_id
                               from   job_history natural join jobs
                               where  job_title = 'Public Accountant');
    
    (Pi_{first_name, last_name} Employees)
    -
    (Pi_{first_name, last_name} (Rou_E(Employees) Join_(E.employee_id = JH.employee_id)
                               Rou_JH(Job_History) Join_(JH.job_id = Jobs.job_id)
                               (Sigma_(job_title = 'Public Accountant') Jobs)))
    
    Result(fn, ln) ::= Employees(eid, fn, ln, _, _, _, _, _, _, _, _)
                   and Jobs(jid, 'Public Accountant', _, _)
                   and NOT Job_History(eid, _, _, jid, _)
    
  5. List the unique country id and country name of each country in which at least one department locates.
  6. List the id and name of each department in which no employee works.