AmitDiwan has Published 10740 Articles

How to speed up SELECT DISTINCT in MySQL

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:44:43

745 Views

To speed up SELECT DISTINCT, you can create an index on the column or set of columns. Let us first create a table −mysql> create table DemoTable (    Name varchar(40) ); Query OK, 0 rows affected (1.13 sec)Following is the query to create an index −mysql> create index Name_Index ... Read More

MySQL query to calculate the total amount from column values with Cost and Quantity?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:37:25

1K+ Views

Let us first create a table −mysql> create table DemoTable (    Cost int,    Quantity int ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(65, 2); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable ... Read More

MySQL query to check if a string contains a word?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:26:27

2K+ Views

For this, you can use the LIKE operator along with CONCAT() function. Let us first create a table −mysql> create table DemoTable (    Value text ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('MySQL'); Query OK, 1 ... Read More

How to select all the records except a row with certain id from a MySQL table?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:24:12

909 Views

To avoid displaying a certain id from a table, you need to use the operator, which is the NOT EQUAL operator. Let us first create a table −mysql> create table DemoTable7 (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(40) ); Query OK, 0 rows affected ... Read More

How to update empty string to NULL in MySQL?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:19:43

1K+ Views

For this, use LENGTH(), since if the length is 0 that would mean the string is empty. After finding, you can set it to NULL using the SET clause in the UPDATE command. Let us first create a table −mysql> create table DemoTable (    Name varchar(50) ); Query OK, ... Read More

Get the first 10 rows followed by the syntax to display remaining row records with a single MySQL query

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:14:05

190 Views

Let us first create a table −mysql> create table DemoTable (    Id int ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values(101); Query OK, 1 ... Read More

What is the alternative of BETWEEN clause to fetch values between a range in MySQL?

AmitDiwan

AmitDiwan

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

1K+ Views

To avoid using the BETWEEN clause, you can use the AND to fetch the values between a range. Let us first create a table −mysql> create table DemoTable (    Number int ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert ... Read More

MySQL query to display the first alphabet from strings in a separate column

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:08:35

19K+ Views

To fetch the first alphabet from the strings, use LEFT(). This method allows you to return characters from the left of the string.Let us first see an example and create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(100) ); Query ... Read More

Change the column name from a MySQL table with Student record?

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:05:16

312 Views

To change the column name, use the AS keyword after the column name. Let us first create a table −mysql> create table DemoTable (    Id int,    StudentFirstNameInCollege varchar(100) ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable ... Read More

MySQL query to select three highest values and sort alphabetically on the basis of corresponding column with name

AmitDiwan

AmitDiwan

Updated on 01-Oct-2019 08:02:35

854 Views

For this, you can use the ORDER BY clause. Let us first create a table −mysql> create table DemoTable (    Name varchar(40),    Score int ); Query OK, 0 rows affected (1.11 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 45); Query OK, ... Read More

Advertisements