I want the row with the single highest col3 for each (col1, col2) pair.
That’s a groupwise maximum, one of the most commonly-asked SQL questions (since it seems like it should be easy, but actually it kind of isn’t).
I often plump for a null-self-join:
SELECT t0.col3 FROM table AS t0 LEFT JOIN table AS t1 ON t0.col1=t1.col1 AND t0.col2=t1.col2 AND t1.col3>t0.col3 WHERE t1.col1 IS NULL;
“Get the rows in the table for which no other row with matching col1,col2 has a higher col3.” (You will notice this and most other groupwise-maximum solutions will return multiple rows if more than one row has the same col1,col2,col3. If that’s a problem you may need some post-processing.)
Related Posts:
- MySQL – Get row number on select
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- Unknown column in ‘field list’ error on MySQL Update query
- Unknown column in ‘field list’ error on MySQL Update query
- MySQL – Operand should contain 1 column(s)
- MySQL “CREATE TABLE IF NOT EXISTS” -> Error 1050
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- MySQL create table if not exists and insert record only if table was created
- How do I use properly CASE..WHEN in MySQL
- How can I do a FULL OUTER JOIN in MySQL?
- MySQL query String contains
- What’s the difference between VARCHAR and CHAR?
- Case statement in MySQL
- How to declare a variable in MySQL?
- MySQL Cannot Add Foreign Key Constraint
- How to split the name string in mysql?
- How do I import an SQL file using the command line in MySQL?
- Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
- mysql Foreign key constraint is incorrectly formed error
- MySQL: Invalid use of group function
- How can I return pivot table output in MySQL?
- Compare dates in MySQL
- MySQL “WITH” clause
- MySQL Multiple Joins in one query?
- Error Code: 2013. Lost connection to MySQL server during query
- MySQL syntax for Join Update
- Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
- how to drop partition without dropping data in MySQL?
- MySQL combine two columns into one column
- Simple way to calculate median with MySQL
- Error 1046 No database Selected, how to resolve?
- MySQL Error 1264: out of range value for column
- Remove duplicate rows in MySQL
- How to SUM and SUBTRACT using SQL?
- Cast from VARCHAR to INT – MySQL
- SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax — PHP — PDO [duplicate]
- SQL select only rows with max value on a column
- How do you force mysql LIKE to be case sensitive?
- MySQL – UPDATE multiple rows with different values in one query
- How to create a MySQL hierarchical recursive query?
- ERROR 1148: The used command is not allowed with this MySQL version
- Insert into a MySQL table or update if exists
- How to round down to nearest integer in MySQL?
- MySQL starts with searching issue
- Query to convert from datetime to date mysql
- Is it possible to GROUP BY multiple columns using MySQL?
- MySQL error: key specification without a key length
- Equivalent of Oracle’s RowID in SQL Server
- Best way to test if a row exists in a MySQL table
- Check if MySQL table exists without using “select from” syntax?
- MySQL equivalent of DECODE function in Oracle
- mysql update column with value from another table
- The used SELECT statements have a different number of columns
- How to delete from multiple tables in MySQL?
- MySQL: Can’t create table (errno: 150)
- How to copy a row and insert in same table with a autoincrement field in MySQL?
- Join vs. sub-query
- Alternative to except in MySQL
- Equivalent of explode() to work with strings in MySQL
- MySQL OPTIMIZE all tables?
- MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET
- MySQL error: Unknown column in ‘where clause’
- How to truncate the text returned for a column in a MySQL query
- Using union and order by clause in mysql
- MySQL LIKE IN()?
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- What is the difference between “INNER JOIN” and “OUTER JOIN”?
- How can I do an UPDATE statement with JOIN in SQL Server?
- How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?
- Drop a temporary table if it exists
- ORA-00918: column ambiguously defined in SELECT *
- Best way to do nested case statement logic in SQL Server
- Get day of week in SQL Server 2005/2008
- mysql Foreign key constraint is incorrectly formed error
- Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?
- Postgresql tables exists, but getting “relation does not exist” when querying
- SQL Parentheses use in an OR clause
- What is the difference between LATERAL JOIN and a subquery in PostgreSQL?
- Repair all tables in one go
- Is it possible to specify condition in Count()?
- SQL Server: Make all UPPER case to Proper Case/Title Case
- How do I query for all dates greater than a certain date in SQL Server?
- Temporary table in SQL server causing ‘ There is already an object named’ error
- Append table to an existing one: SQL Server
- How to order by column A and then by column B?
- SQL Server – An expression of non-boolean type specified in a context where a condition is expected, near ‘RETURN’
- register_post_status – show_in_admin_all_list & show_in_admin_status_list does not affect query
- How to get the list of WooCommerce product image of a certain category from database?
- prepare() not working
- How do I move a WordPress site to another server?
- How do I get the posts within a certain year/date(with sql query)?
- how to insert missing tags into the posts through mySQL?
- SQL query to set posts in bulk based on the post content
- DB_HOST – is LOCALHOST speedy than domain name? [closed]
- JOIN and SUM different statement results (Mailster Database)
- Performing CRUD operations on front end in wordpress
- How to show result of sql query in a page?
- blank page after update my theme
- SQL command to export post_content from wp_posts using phpMyAdmin
- Adding profile data to database