Rename Table in SQL

Sometimes, it may be necessary to rename a table. There is no standard way to rename a table, and the implementation varies by RDBMS. Below we discuss how we can rename a table in MySQL, Oracle, and SQL Server.

MySQL

In MySQL, we can rename a table using one of the following methods:

Method 1

RENAME OLD_TABLE_NAME TO NEW_TABLE_NAME

Method 2

ALTER TABLE OLD_TABLE_NAME
RENAME TO NEW_TABLE_NAME

Continue reading

Find 2nd Largest Value Using SQL

We all know that the MAX function can be used to find the largest value in SQL. How, then, can we write a single-pass SQL that can be used across different database systems to find the second largest value in a column? Single-pass means only one SQL query gets executed, as opposed to having multiple SQL statements using temporary tables to store intermediate results.

Continue reading