SQL how can i get sum,avg and count of my table? - sql

I have a table with 3 columns: name,continent and population. I need to make a new table with the sum of countries, avg of population , and sum of population.
this is my code but i get an error
SELECT COUNT(name) AS number,
AVG(population) AS average,
SUM(population) AS total FROM coutries;
this is my error
ERROR 1146 (42S02) at line 99: Table 'ri_db.coutries' doesn't exist

Your table's name is countries and there is no column countries, so do this:
SELECT
COUNT(*) AS number,
AVG(population) AS average,
SUM(population) AS total
FROM countries;

"from coutries" should probably be "from countries".
And as mentioned in a comment use "count(*)".

"I have a table with 3 columns: name,continent and population."
Then if you want the total count of rows, you need COUNT(1) or COUNT(*). COUNT(countries) will not work: there is no column called 'countries'.

Related

SQL How to select data (countries) that only has code with 3 characters?

I have to extract all the list of countries. however in the datatable under country column (2nd column), it includes continent as well, which is not what i want.
I realised those with the first column (iso_code) with only 3 characters are data with countries, while those with more than 3 characters are continents/non country. How do i go about extracting this?
this is my current code when extracting everything (including continent that are under country column) :
select count(distinct country) as "Number of Countries"
FROM owid_energy_data
if you are looking to extract all the distinct names of countries from a column where you have first three letters always as a country name then use this:
select distinct left(country,3) as country from owid_energy_data
selecting the countries, where the iso_code (first column) has only three letters:
select distinct country as countries from owid_energy_data where length(iso_code) = 3
if you want the count then simply add count to one of the above:
select count(distinct country) as total_countries from owid_energy_data
Like this:
select count(distinct country) as "Number of Countries"
FROM owid_energy_data
where len(iso_code) = 3
SELECT OED.COUNTRY AS [NUMBER OF COUNTRIES]
FROM OWID_ENERGY_DATA OED
WHERE LEN(OED.COUNTRY) < 3
Try that!

How do i count number of consultated

/16 Write a query to show the Staff Id, number of consultations. Name the new column Num of Consults./
COLUMN COUNT(dateconsulted) HEADING "Num of Consults"
SELECT staffid,
COUNT(dateconsulted)
FROM consultation;
ERROR at line 1: ORA-00937: not a single-group group function
it should count how many number of consults that has been completed
Edited i got it to work i hope but the next one is similar
/* Write a query to show the Staff id, number of consultations only for staff that have more than 2 consulation Name the new column Num of Consults
*/
/*come back to this double checking after */
COLUMN COUNT() HEADING "Num of Consults"
SELECT staffid,
COUNT()
FROM consultation
WHERE 'Num of Consults'> 2
GROUP BY staffid;
Result that i wanting is to just disply the Num Of consults that is greater > than 2 but for some reason it keep other record when i want to display just the record of more than 2
Please try this.
SELECT
staffid, COUNT(dateconsulted) as "Num of Consults"
FROM consultation
GROUP BY staffid;

HAVING and GROUP BY

The problem I am trying to solve is as follows
For each continent show the continent and number of countries with populations of at least 10 million
The world table looks like this:
World (name, continent, area, population, gdp)
My query (which is not returning the correct result):
SELECT continent, COUNT(name)
FROM world
GROUP BY continent
HAVING sum(population) >= 10000000
Query returning the correct result:
SELECT continent, COUNT(name)
FROM world
WHERE population >= 10000000
GROUP BY continent
Can someone tell me why my query is wrong?
The question is about countries, not continents, so you need to do the filtering before aggregation.
Your version of the query is answering:
How many countries are in continents whose population is greater than 10,000,000?
The question is:
How many countries in each continent have a population greater than 10,000,000?
These are different questions. I also realize that for non-fluent English speakers, the difference may not be obvious at first read.

sql count or sum?

I'm a newbie programmer, I want to sum a value of employee's attendance record
Anyway, what should I choose? COUNT or SUM?
I tried to use COUNT functions like this...
SELECT COUNT(jlh_sakit) AS sakit FROM rekap_absen
It shows value changed to "1" for 1 Record only.
And I try to use SUM functions like this...
SELECT SUM(jlh_sakit) AS sakit FROM rekap_absen
It shows all values changed ALL value to "1"
I want to display only 1 person for each sum
(e.g : John (2 sick, 2 permissions, 1 alpha)
Can you help me please?
If you are using any aggregate function like min/max/sum/count you should use group by. Now your question says "what should I choose? COUNT or SUM?" Assuming you have person_name, jlh_sakit which means sick/permission/alpha in your case you could use
select person_name, count(jhl_sakit) as attribute
from rekap_absen
group by person
This will give you output like:
person_name attribute
John 2
King 5
In order to sum by specified column, use group by statement.
SELECT SUM(sick),SUM(alpha),SUM(permissions),person FROM rekap_absen group by person
It will group your sums according to person.
You may name your sums like:
SELECT SUM(sick) as sick,SUM(alpha) as alpha,SUM(permissions) as permissions,person FROM rekap_absen group by person
Assuming that you have table rekap_absen with columns: person,sick,alpha,permissions

Where Function in sqlite

Basically I have so far managed to produce this:
sqlite> SELECT city, COUNT(bname) AS sum FROM building GROUP BY city;
city sum
---------- ----------
Leeds 3
London 1
New York 2
Paris 1
However what I want to do is only print the Cities that have a sum > 1.
I have tried this:
sqlite> SELECT city, COUNT(bname) AS sum FROM building WHERE sum>1 GROUP BY city;
But I get the error:
Error: misuse of aggregate: COUNT()
Can someone explain why this isn't working, an what I shoud do instead?
Thanks
When you want to limit your resultset based on an aggregate function you use the HAVING clause instead of where:
SELECT city,
COUNT(bname) AS sum
FROM building
GROUP BY city
HAVING COUNT(bname) > 1;
There are good explanations on stackoverflow on why you can't use WHERE, but here's a short one:
Here's what happens when you do a SELECT:
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
(This list can be bigger in some RDBMS, but this should give you an idea)
Because you are doing a GROUP BY to get your COUNT, you can't limit that on WHERE clause because it already happened.
You need to use having when filtering based on the result of an aggregate function
SELECT city, COUNT(bname) AS sum
FROM building
GROUP BY city
HAVING COUNT(bname) > 1;