mysql min and max values in subquery - mysql

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)

Related

Very basic question about dividing two MySQL table columns

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.

Mysql not working when used double alias

Hey guys am really new to mysql.I have heard of alias in mysql and i have tried double alias with the string like
select ('name' as bae,'age' as ages) as person;
When i run the above code it doesnt give me the output and raises the error.I dont understand why the double alias didnt works in mysql.
Any help to make this one correct would be really appreciated..Thanx in advance
The problem with your query are the parentheses. They represent a single expression and you don't put aliases (or commas usually) inside expressions. So, this fixes the immediate problem:
select 'name' as bae, 'age' as ages
I'm not sure what the second "as person" is supposed to be. Perhaps you want a subquery:
select person.*
from (select 'name' as bae, 'age' as ages) person
A really basic MySQL query should look something like:
SELECT
[COLUMN_NAME] AS [ALIAS_NAME]
FROM
[TABLE_NAME]
Based on your query it appears you are trying to retrieve the two columnns name and age from the person table.
If this is the case, the following query can help you:
SELECT
name AS bae,
age AS ages
FROM
person
But, this is still just one big guess, if you really want us to help you with your problems, you should give some more information about what you are trying to achieve.

Why is my count function only working on certain tables?

I know this is an amateur question, but I've searched every resource I can think of, and now I'm at my wit's end. The following query works perfectly on most of my tables, but for some reason it is not working on the tables that I desperately need it for:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE daypart LIKE 'AM';
The query works exactly how I want it to for nearly all of my tables, but for some reason it is returning a value of "0" on the tables I need it for (even though there are over 20 instances of "AM" in the "daypart" column on this table). I have checked and double-checked everything I can think of.
I'm relatively new to SQL but I've never encountered a problem like this before. Anyone have any ideas or resources that might help? Thanks so much for your time!
EDIT: I don't have enough reputation points to post a screen shot on here... but here's a link where you can see one: http://imgur.com/ZhyEqJY
There are 29 columns in this table. If there's any other info that might help just let me know, thanks!
You need to add the like part as shown below:
where column_name like '%AM%'
when you write like 'AM' it is searching for the full match
Try this.
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
If you want to order it using the count,
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%' ORDER BY COUNT(*) DESC;
DESC - Descending order
ASC - Ascending order
LIKE statements are really slow in sql. If you have a daypart column then I assume you have a date column. I recommend something like this instead:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE HOUR(dateColumn) < 13
Unless the string you're matching is exactly AM you need to use wildcard characters to match the string:
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
The % matches any number of characters before and after AM.
See the documentation for more information.

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...