Found 6702 Articles for Database

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

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

17K+ 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 OK, 0 rows affected (1.03 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName) values('Adam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(FirstName) values('Jace'); Query OK, 1 row affected (0.16 sec) ... Read More

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

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

194 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 values(100, 'Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(101, 'Bob'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

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

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

676 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, 1 row affected (0.26 sec) mysql> insert into DemoTable values('Bob', 98); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David', 78); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Mike', 96); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Carol', 43); ... Read More

Adding records with REPLACE INTO to mimic DELETE and INSERT

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

110 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 id_index(Id); Query OK, 0 rows affected (0.41 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command. Since we have added duplicate records, the new record gets added i.e. replaced with the same Id with the previous record −mysql> replace into DemoTable values(100, 'Chris'); Query ... Read More

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

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

114 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 ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1(Score1) values(56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1(Score1) values(76); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1(Score1) values(65); Query OK, 1 row affected ... Read More

Can we skip column when inserting into MySQL?

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

604 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 some records in the table using insert command. Here, we have skipped the first column, since it is AUTO_INCREMENT −mysql> insert into DemoTable values(NULL, 'Robert', 21); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(NULL, 'Sam', 22); Query OK, 1 row affected (0.18 sec) mysql> insert into ... Read More

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

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

65 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 −mysql> insert into DemoTable values(1, 'Chris', 23); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable values(2, 'David', 21); Query OK, 1 row affected (0.56 sec) mysql> insert into DemoTable values(3, 'Mike', 24); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(4, 'Robert', 22); Query ... Read More

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

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

756 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 values(50, 'Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(51, 'David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(51, 'Mike'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(50, 'Sam'); Query OK, 1 row affected (0.17 sec)Display all records ... 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
Updated on 01-Oct-2019 07:46:13

399 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 values(999, 'Robert'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values(1003, 'Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1 values(1002, 'Sam'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;This will produce the ... Read More

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

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

215 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 sec) mysql> insert into DemoTable values(97); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(91); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(86); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.12 sec)Display all ... Read More

Advertisements