SELECT column1 FROM table WHERE column2 is greater than column1 - mysql

I want to display all values of a column that is greater than its equivalent value in another row
Like for example,
Display country name WHERE population is greater than country name (i.e. Andorra)
SELECT name FROM country
WHERE population > population (of Andorra)

You can use subquery for this, e.g.:
SELECT name
FROM country
WHERE population > (
SELECT population
FROM country
WHERE name = 'Andorra'
);
Please note that this query will return an error if
there is no record with name country name 'Andorra' or
there is more than one record with country name 'Andorra'

SELECT name FROM country
WHERE population > (SELECT population FROM country WHERE name = 'Andorra')
You think something like this?

Related

the sum of fields values from a column with a condition

I am quite new to mySQL and need help.
I have a database like this (values are example):
name |region |population|
----------------------------------
Cuba |Carribean |10 |
Ukraine|Eastern Europe|15 |
Belarus|Eastern Europe|9 |
Haiti |Carribean |3 |
I want to find total population of the region (e.g. all population from Eastern Europe) and print as a table the region name and its total population.
But I have no idea how to find a sum of field value from one column but under condition from another column.
How to to do this query?
SELECT region, SUM(population) AS population
FROM table
GROUP BY region
Try below query:
select name , region , sum(population)
from region_table // table_name
group by name , region
May be this will help you.

How do i copy data from one table to another table?

I have 2 tables, country and division with the ff schemas:
country division
----------- ------------
countrycode divisioncode
contryname countrycode
divisionname
I want to copy data from the country table into the division table where countrycode goes into divisioncode and countrycode in division table and countryname going into divisionname
For ex, US - United States goes into the division table as US, US, United States.
Use INSERT INTO ... SELECT
Query
INSERT INTO division(divisioncode, countrycode, divisionname)
SELECT countrycode, countrycode, countryname
FROM country;
insert into division (divisioncode, countrycode, divisionname)
select t1.countrycode, t1.countrycode, t1.countryname
from country t1
See: http://dev.mysql.com/doc/refman/5.7/en/insert-select.html

How to sort based on keyword first?

Here is a sample Database
First_name country
Andy US
Manu India
Paul Pakistan
Ramesh Pakistan
Rich India
So, what i want is to select all records from the above table and display according to name.
Like :-
I want to select person name to be display first whose country name is India and after US, Pakistan.
How can i perform this task in single SQL query ?
Update
I don't know how many Country are there.
Country the need to be display first will be input by the user.
Use a CASE statement to give each record a sortkey. 0 for a country match, 1 for a mismatch, so the desired country comes first.
select *
from mytable
order by case when country = #country then 0 else 1 end, first_name
May be something like this
Select * From Table1
Order By CASE WHEN country = 'INDIA' THEN 0
WHEN country = 'US' THEN 1
Esle 2
END;
Or You can use FIELD
Select * From Table1 Order By FIELD(country, 'India', 'US', 'Pakistan') ;
Use FIELD Function
Try this:
SELECT fitst_name, country
FROM tableA
ORDER BY FIELD(country, 'India', 'US', 'Pakistan'), fitst_name

MYSQL query for a table

I have a table called visitors which contains IP and country columns.
Now I want to query the table such that I get unique countries from the table and display the count of rows of that country.
Table:
IP Country
1.1.1.1. xyz
1.2.3.4 xyz
2.2.3.6 abc
3.61.3.69 axy
Now I want the result as :
Country No_Visitors
xyz 2
abc 1
axy 1
I know how to do it by using 2 queries, get the unique country first and then again query the table for the country name. But how can I do it with single query.
use AGGREGATE FUNCTION COUNT() to get the total number of instances for each country.
SELECT Country, COUNT(IP)
FROM tableName
GROUP BY Country
SQLFiddle Demo

SQL Query to provide me with the total number

I have a table with 3 columns
Column 0: auto inc index
Column 1: Person's Gener
Column 2: The STATE the person lives in (US States and Puerto Rico)
I want to run a query that tells me how many MEN are in each state
so the output would list all 50 states (in 1 column) and the second would be a number to determine the number of MEN in that state
Alaska 1000
New York 85000
(the figures above aren't accurate but i'm illustrating what I am looking for)
Thanks
You need to use a GROUP BY clause and the COUNT aggregate function.
SELECT State, COUNT(*) AS NumberOfMen
FROM your_table
WHERE Gender = 'M'
GROUP BY State
try this
select STATE, count(*) as num from table_name where gender ='MALE' GROUP BY STATE;