AmitDiwan has Published 10740 Articles

Adding records with REPLACE INTO to mimic DELETE and INSERT

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:00:10

172 Views

You can use REPLACE INTO that works like DELETE + INSERT. Let us first create a table −mysql> create table DemoTable (    Id int,    FirstName varchar(50) ); Query OK, 0 rows affected (0.60 sec)Following is the query to create a unique index −mysql> alter table DemoTable add unique ... Read More

A single MySQL query to find the highest and lowest among two tables?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 07:55:54

201 Views

To find the highest and lowest from two tables, use MAX() and MIN(). Since the results are to be displayed from two tables, you need to use UNION. Let us first create a table −mysql> create table DemoTable1 (    UniqueId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Score1 int ... Read More

Can we skip column when inserting into MySQL?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 07:52:50

789 Views

If your first column is AUTO_INCREMENT, then you can skip the column and place the value NULL. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(100),    StudentAge int ); Query OK, 0 rows affected (0.60 sec)Insert ... Read More

How to optimize many SELECTs in a single table in MySQL?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 07:51:18

109 Views

To optimize many SELECTs, use it once and apply IN() to fetch multiple values. Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100),    Age int ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command ... Read More

MySQL query to count the duplicate ID values and display the result in a separate column

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 07:48:25

1K+ Views

To count the duplicate ID values, use aggregate function COUNT() and GROUP BY. Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (1.30 sec)Insert some records in the table using insert command −mysql> insert into DemoTable ... Read More

Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 07:46:13

535 Views

Let us first create a table −mysql> create table DemoTable1 (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.86 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(1001, 'Chris'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1 ... Read More

Calculate average of column values and display the result with no decimals in MySQL

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 07:42:23

335 Views

For this, you can use round() along with avg(). Let us first create a table −mysql> create table DemoTable (    Score int ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.22 ... Read More

What is the difference between TINYINT(1) and Boolean in MySQL?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 07:26:15

3K+ Views

There is no difference between TINYINT(1) and Boolean. The keyword Bool or Boolean internally converts into TINYINT(1) or we can say Bool or Boolean are synonymous with TINYINT(1).Let us first create a table −mysql> create table DemoTable (    isMarried Boolean ); Query OK, 0 rows affected (1.77 sec)Let us ... Read More

Easiest way to copy values of one column to a new table in MySQL?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 07:23:55

177 Views

For this, use AS select statement. Let us first create a table −mysql> create table DemoTable1 (    Score int ); Query OK, 0 rows affected (1.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(89); Query OK, 1 row affected (0.14 sec) mysql> insert ... Read More

Find minimum score from the entire four columns of a table in MySQL

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 07:22:14

157 Views

To find a minimum score from the entire four columns, use MySQL LEAST() function. Let us first create a table −mysql> create table DemoTable(    Score1 int,    Score2 int,    Score3 int,    Score4 int ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using ... Read More

Advertisements