MySQL function that counts occurrences - mysql

So I have a table with the following columns: id, Fname, Lname, city and desc
What I am trying to accomplish is to create a function that counts how many employees are working in each city.
Normally I would just use something like
select city, count(*) as NumofEmp from employee group by city
but at the moment I have to create a function for this. How could I accomplish this?

select city, count(name) as NumofEmp from employee group by city

Related

Count number of customers in each Distinct city from customer table in sql

My database has a table named customer with data in the following columns: id, first_name, last_name, and city.
Like this:
Now I need a table with the number of customers in each Distinct city. what should the SQL query for it?
You can use COUNT aggregate function -
SELECT city, COUNT(ID)
FROM customer
GROUP BY city;

Mysql query with more than one aggregate function

I have this table:
EMPLOYEES(Code, Name, Surname, Type, Department, Salary)
I need to display the department with the HIGHEST total expense for salaries WITHOUT using a view. Is that possible? The solution with a view is this:
CREATE VIEW DEPEXPENSES (DEPNAME, EXPENSE) AS
SELECT DEPARTMENT, SUM(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT
SELECT DEPNAME
FROM DEPEXPENSES
WHERE EXPENSE=(SELECT MAX(EXPENSE) FROM DEPEXENSES)
Thanks.
The question is not too clear, but I think you want to run your second query without using a view. One solution is this:
select
DEPARTMENT,
SUM(SALARY)
from
EMPLOYEES
group by
DEPARTMENT
having
SUM(SALARY)=(
select MAX(SALARY) from (
select DEPARTMENT, SUM(SALARY) as SALARY
from EMPLOYEES
group by DEPARTMENT
) s
)
(you can apply a max aggregated function on a subquery that already contains an aggregated function)

use for COUNT command to create query in MYSQL

Below shows my original MY SQL table.
SELECT name, COUNT(city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
I need following result.
SAM has 4 records. But there are city of London in two records.
I need How many different cities in SAM's Records.
I did lots of queries, but I couldn't create my SQL query.
Add DISTINCT to the count parameter:
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;

Use of COUNT to display distinct records with more than 2 occurrences in MySQL

I have a table where I am trying to build a distinct list of all the cities with more than two occurrences in the table. I am trying the current query am am told "function count does not exist"? What am I doing wrong?
SELECT COUNT (city)
FROM `table1`
GROUP BY city
HAVING COUNT (city) >=2
Your query is correct you have given a space between COUNT and (City) it must be COUNT(City). that will work fine. Your query should be like this:
SELECT City, COUNT(city) Counts
FROM `table1`
GROUP BY City
HAVING COUNT(city) >=2;
See this SQLFiddle
USE ALIAS
SELECT COUNT(city) as SOME_TEXT FROM table1 GROUP BY city HAVING SOME_TEXT >=2

Repeating column names when outputting a query

Is it possible to repeat the names of a database table's columns when executing a query whenever the GROUP BY info change? My query is group by a column.
SELECT name.fname as First_Name,
name.sales as Sales,
name.city as City
FROM name
GROUP BY city
ORDER BY name.fname;
I think you want this:
SELECT name.fname as First_Name,
name.sales as Sales,
name.city as City
FROM name
ORDER BY city, fname;