Find the company that has the smallest payroll

Hi i have the following database scheema.

employee (employee-name, street, city)
works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)

I need to find Find the company that has the smallest payroll.

  • How to create comma delimited list from table with dynamic columns
  • SQL: Inner joining two massive tables
  • Error Code: 1406. Data too long for column - MySQL
  • Query to loop through one table and insert incrementally by group into another
  • Show 1 Count for Percentage
  • Could not write value to key \SOFTWARE is displayed for SSMS Install in a Windows 7 machine with an Administrator account
  • I have written the following query but my trainer tells me its wrong.

    SELECT company-name
    FROM works
    GROUP BY company-name
    HAVING sum(salary) < ( SELECT sum(salary)
    FROM works
    GROUP BY company-name)
    

  • Create custom “auto-increment” Compound Primary Key?
  • PHP and SQL Shopping Cart is Not Adding Products to Shopping Cart
  • How to sum 8 rows at a time in a select statement
  • Would a query run in a try statement that failed if it was before the error occured?
  • How can I insert/update rows with C# to SQL Server
  • SQL Full text Search with phrases, prefix_term on multiple columns
  • 3 Solutions collect form web for “Find the company that has the smallest payroll”

    Your subquery

    SELECT company-name, sum(salary)
    FROM works
    GROUP BY company-name
    

    will give you the cumulative salaries (payrolls) for each company.
    If you order those data in ascending order, the first record will be the company with the smallest payroll.

    SELECT company-name, sum(salary)
    FROM works
    GROUP BY company-name
    ORDER BY sum(salary)
    

    You can than use MySQL’s LIMIT clause to restrict that recordset just to a single record, using

    SELECT company-name, sum(salary)
    FROM works
    GROUP BY company-name
    ORDER BY sum(salary)
    LIMIT 1
    

    I’ll give you a further hint. The < is what’s wrong (think carefully about what the subquery returns).

    we can use < All in this case?

    e.g. :

    SELECT company-name
    FROM works
    GROUP BY company-name
    HAVING sum(salary) < ALL( SELECT sum(salary)
    FROM works
    GROUP BY company-name)
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.