Advertisement
Guest User

Untitled

a guest
Aug 1st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.62 KB | None | 0 0
  1. /*
  2. create database mycompany;
  3. go
  4. use mycompany;
  5.  
  6. create table locations (
  7.  id int,
  8.  city varchar(255),
  9.  country varchar(255),
  10.  addr varchar(255)
  11.  
  12.  primary key (id)
  13.  )
  14.  
  15. create table employee (
  16.  id int,
  17.  boss int,
  18.  name varchar(255),
  19.  division varchar(255),
  20.  location_id int,
  21.  
  22.  primary key (id),
  23.  foreign key (location_id) references locations (id)
  24.  )
  25.  
  26. create table wages (
  27.  employee_id int,
  28.  wage int
  29.  
  30.  foreign key (employee_id) references employee (id),
  31.  )
  32.  
  33.  insert into locations (id, country, city, addr) values
  34.  (1, 'canada', 'toronto', 'mccowan'),
  35.  (2, 'saudi arabia', 'madina', 'sultana'),
  36.  (4,'canada', 'toronto', 'brimley'),
  37.  (22, 'usa', 'washington', 'pennsylvania av')
  38.  
  39.  insert into employee (id, boss, name, division, location_id) values
  40.  (1, NULL, 'joe', 'management', 2),
  41.  (44, 1, 'ryan habibi', 'IS', 1),
  42.  (11, 44, 'abdullah', 'IS', 4),
  43.  (2, 11, 'tommy', 'management', 22),
  44.  (3, 11, 'dan', 'IT', 4),
  45.  (5, 2, 'zair', 'janitorial services', 1)
  46.  
  47.  insert into wages (employee_id, wage) values
  48.  (1, 4000),
  49.  (44,  12000),
  50.  (11, 200),
  51.  (2, 8),
  52.  (3,  20),
  53.  (5, 1000)
  54.  */
  55.  select employee.id, employee.name, wages.wage
  56.   from  wages join employee on wages.employee_id = employee.id
  57.   where wages.wage = (select max(wage) from wages)
  58.  
  59.   select employee.location_id,  count(*)
  60.   from employee  join locations on employee.location_id = locations.id
  61.     group by employee.location_id
  62.  
  63.   select count(*),locations.city
  64.   from employee  join locations on employee.location_id = locations.id
  65.     group by locations.city
  66.  
  67.  select boss  , count(*)
  68.  from employee as e1
  69.  group by boss
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement