Finding duplicate values in a SQL table

Simply group on both of the columns. Note: the older ANSI standard is to have all non-aggregated columns in the GROUP BY but this has changed with the idea of “functional dependency”: In relational database theory, a functional dependency is a constraint between two sets of attributes in a relation from a database. In other words, … Read more

How to declare a variable in MySQL?

There are mainly three types of variables in MySQL: User-defined variables (prefixed with @):You can access any user-defined variable without declaring it or initializing it. If you refer to a variable that has not been initialized, it has a value of NULL and a type of string.SELECT @var_any_var_name You can initialize a variable using SET or SELECT statement:SET @start = 1, @finish = … Read more

Rename a column in MySQL

Use the following query: The RENAME function is used in Oracle databases. @lad2025 mentions it below, but I thought it’d be nice to add what he said. Thank you @lad2025! You can use the RENAME COLUMN in MySQL 8.0 to rename any column you need renamed. ALTER TABLE Syntax: RENAME COLUMN: Can change a column name but not its definition. More … Read more

Case statement in MySQL

Yes, something like this: As other answers have pointed out, MySQL also has the IF() function to do this using less verbose syntax. I generally try to avoid this because it is a MySQL-specific extension to SQL that isn’t generally supported elsewhere. CASE is standard SQL and is much more portable across different database engines, and I prefer to … Read more