Very basic question about dividing two MySQL table columns - mysql

So, I'm just beginning learning SQL today. I have a table in MySQL and need to find the gdp per capita and make a new column out of it (or potentially just assign it to a column I have already made if making a new one isn't possible), I have a column with the countries' gdp and the countries' population, both of which have been given the type BIGINT. But how might I divide those two numbers together and have them in descending order and I want to print them out?
The code I have so far is:
SELECT countries_name, countries_population, countries_gdp
FROM countries.countries
WHERE (countries_population / countries_gdp) countries_per_capita_gdp
ORDER BY DESC
Would anyone be able to give me a hint as to what I might be doing wrong please? Our lecturer is giving us very little guidance on this so I think most of us are confused, unfortunately.

I think what you're looking for is this:
SELECT countries_name, countries_population, countries_gdp, (countries_population / countries_gdp) AS countries_per_capita_gdp
FROM countries
ORDER BY countries_per_capita_gdp DESC
I noticed your FROM had countries.countries, not sure if that was intentional, might just have to be countries.

Related

mysql min and max values in subquery

thanks in advance for the help. I'm a noob with sql and this is probably a pretty basic question, but I have been working on it for hours and can't seem to figure out what I'm doing wrong.
Here is the question posed:
Using aggregate functions, display the employee name (first and last name concatenated) in a field named Employee Name and the hire date of the employee who has been employed the longest and the employee who was hired last.
I have tried to code this different ways to no avail. I always get one result instead of the two I know I should get. (I know I should get two results because I'm working with a very small table and can calculate the answer by looking at it - easy to make sure I'm getting the right results that way.)
Here is one way I coded it:
subquery
Here, I used a sub query for the min and max. I used 'and' in between because I want both results. I only get one. If I use 'or', I get too many results
I have also tried it this way:
min/max in select statement
Here, I still get only one employee name back, but now I get both dates. The dates are the correct dates, but I need the output to look like the first query output.
Any help anyone can provide would be greatly appreciated!
How about
select concat(first_name,' ',last_name), hire_date
from l_employees
where hire_date = (select max(hire_date) from l_employees)
or hire_date = (select min(hire_date) from l_employees)

How to alphabetically sort the columns in a table?

So my boss asked us to sort the columns alphabetically so the clients will have an easier time locating the column they want. The pictures below show what I mean by that.
Currently, the columns are shown in an order like this: original layout
However, what I am trying to achieve is like this: sorted columns
As you can see the columns are sorted alphabetically but I did that manually. I am trying to figure out a way to let the code do it. Even though it sounds like a fairly simple goal, it has taken me tons of time and all my approaches turn out to be nothing but dead ends. I am running my codes on Periscopedata.com and I think they use Redshift by Amazon.
The engineers at Periscopedata helped me a lot, and their approach is this:
CREATE TABLE #ordercolumns as SELECT attname::varchar as column_name
from pg_attribute WHERE attrelid='devices'::regclass::oid and
attnum>=1;
SELECT 'SELECT ' || LISTAGG(column_name, ',') within group (order by
column_name) || ' FROM devices' FROM #ordercolumns;
DROP TABLE #ordercolumns;
I was wondering if I could do something like:
select (select * from ordercolumns) from devices
Sorry if I didn't explain my questions well enough.
I wanna thank you guys ahead of time as this community has helped me tremendously since I was in college.
update: I wanna thank you for all your help. We try to avoid any type of manual sorting because we have hundreds of clients and each client has different columns they want to see. So sorting columns manually is going to take too long with very little accuracy. thank you
You could rearrange the order of the columns with something like this:
ALTER TABLE `devices`
CHANGE COLUMN content content VARCHAR(255) FIRST,
CHANGE COLUMN date date DATE AFTER `content`,
CHANGE COLUMN id id INT(11) AFTER `date`,
....
;
Just be sure to check the data types of each column and as always make a backup of the table before making any changes.

SQL query and for loops

I need a bit of help with a MySQL query. Right now I have a table that has 3 columns: location, street, and number. I want to write a query whose pseudocode would be...
for each location:
for each of the streets of a location
find the next biggest number
I feel like I am still thinking in "for-loop" logic, but that doesn't really seem to be the way MySQL operates, and it is tripping me up. Any help is greatly appreciated!
Also the primary key is the location-street combo
If I understand you right, you want the highest number of each street in each location?
SELECT
location,
street,
MAX(number)
FROM table
GROUP BY
location,
street
First of all, GROUP BY aggregates ("collapses") rows, so that only one row per location and street will be returned. In the list of rows to return, we then explicitly specify we want the highest number (MAX()) instead of the first one that would show up.
http://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html
http://dev.mysql.com/doc/refman/5.6/en/example-maximum-column.html
http://dev.mysql.com/doc/refman/5.6/en/select.html
If it helps, you can think of a SELECT query (albeit very crudely) as filter rules for a loop that goes through every row of the entire table. In these "filter rules" you specify precisely what you want and how you want it, MySQL will then iterate over the entire table and handpick exactly the data you asked for.

Retrieving two highest values from one column for ranked comparison of difference

MODIFIED TO ADD INFORMATION:
I realize that there have been many "get two highest" or "get second-highest" SQL questions and answered posted, so I apologize in advance if this question is redundant, but I want to do something a bit different than the other situations, and I need some help getting from A to B. I am a MySQL hobbyist at best, so I'm sure the answer is obvious to some of you.
I have a bunch of rows of baseball player single-season statistics. I want to compare their season with the highest value with their season with the second highest value. I also want to be able to compare the two seasons by subtracting the second-highest from the highest.
I can easily get the highest value using MAX, of course, but this is a big more difficult for a novice like myself.
Thanks for your help so far.
I will simplify the relevant table structure so that it is relevant:
playerid, Year, Value
Each Player-season is separated by year.
What i want returned from my query is
Player id,
Year [of Highest Value],
Value [Highest],
Year [of Second Highest Value],
Value [Second-Highest]
I hope that is simple enough and clear. Thanks for any help.
Without knowing your table structure, you could essentially do:
SELECT score
FROM statstable
WHERE playerID=???
ORDER BY score DESC
LIMIT 2
which would retrieve the two rows with the highest scores, which you can the pull out the scores and subtract in your client.
If you need this highest-next_highest value for user in another query, then it gets a bit more complicated.

De-dupe a list of hundreds of thousands of first name/last name/address/date of birth

I have a large data set which I know contains many dupicate records. Basically I have data on first name, last name, different address components and date of birth.
I think the best way to do this is to use the name and date of birth as chances are if these things match, it's the same person. There are probably lots of instances where there are slight differences in spelling (like typos missing a single letter) or use of name (ie: some might have a middle initial in first name column) which would be good to account for, but I'm not sure how to approach this.
Are there any tools or articles on going about this process? The data is all in a MySQL database and I have a basic proficiency in SQL.
You could get a sense of how much dedupe you have to do by something like:
select birthDate,last_name,soundex(first_name),count(*)
from table
group by birthDate,last_name,soundex(first_name)
having count(*) >1
This will list the people with the same birthdate, last_name, and similar first names. Soundex() isn't great, but this could help you get a sense of amount of deduping.
This query below would allow you get the alphabetical first first_name from the table of similar named people. Hopefully this will give you some rough starting ideas//
select birthDate,last_name,soundex(first_name),min(first_name)
from table
group by birthDate,last_name,soundex(first_name)
having count(*) >1
With the second query, you could remove all occurrences of additional names, by using a DELETE where name not in, but that assumes you are willing to keep the lowest first_name and remove the rest...