AmitDiwan has Published 10740 Articles

MySQL edit and update records including employee salary

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:58:04

555 Views

The UPDATE command is used in MySQL to update records. With it, the SET command is used to set new values. Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeName varchar(50),    EmployeeSalary int ); Query OK, 0 ... Read More

Does using SERIAL as column name already includes 'NOT NULL' in MySQL?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:54:35

218 Views

In MySQL, SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE. So, do not use NOT NULL after SERIAL, since it already includes NOT NULL as part of its definition.Let us see an example and create a table. Here, we have a column with the name “serial” −mysql> ... Read More

MySQL query to select the nth highest value in a column by skipping values

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:53:05

219 Views

To get the nth highest value in a column, you can use LIMIT OFFSET. Here, OFFSET is used to skip the values. Let us first create a table −mysql> create table DemoTable (    Value int ) ; Query OK, 0 rows affected (0.49 sec)Insert some records in the table ... Read More

How to delete rows older than 14 days in MySQL?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:51:08

1K+ Views

To delete, use MySQL DELETE. However, to get records older than 14 days, subtract the current date with date interval of 14 days. The syntax for the same is shown below −delete from yourTableName where yourColumnName< (curdate() - interval 14 day);Let us first create a table −mysql> create table DemoTable ... Read More

Create a table inside a MySQL stored procedure and insert a record on calling the procedure

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:49:06

628 Views

Create a table inside the stored procedure and use INSERT as well −mysql> DELIMITER // mysql> CREATE PROCEDURE create_TableDemo(id int, name varchar(100), age int)    BEGIN    CREATE TABLE DemoTable    (       ClientId int NOT NULL,       ClientName varchar(30),       ClientAge int,   ... Read More

How to find last date from records with date values in MySQL?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:47:01

321 Views

To get the last date i.e. the latest, use aggregate function MAX() with a subquery. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ExpiryDate date ); Query OK, 0 rows affected (1.40 sec)Insert some records in the table ... Read More

Is it compulsory to set PRIMARY KEY for AUTO_INCREMENT value?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:45:40

217 Views

Yes, use AUTO_INCREMENT with PRIMARY KEY. Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT,    EmployeeName varchar(40),    EmployeeAge int,    PRIMARY KEY(EmployeeId),    UNIQUE KEY(EmployeeName, EmployeeAge) ); Query OK, 0 rows affected (0.96 sec)Let us check the table description of ... Read More

MySQL query to find the average of only first three values from a column with five values

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:42:16

265 Views

For this, you can use a subquery. Let us first create a table −mysql> create table DemoTable (    Score int ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.13 sec) mysql> ... Read More

MySQL query to display record with maximum count values in a group with other column values?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:40:23

429 Views

For this, use the GROUP BY HAVING clause. Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.22 sec) ... Read More

How to count specific comma separated values in a row retrieved from MySQL database?

AmitDiwan

AmitDiwan

Updated on 04-Oct-2019 06:38:12

525 Views

To count comma-separated-values, use aggregate function COUNT(*) along with FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable (    Value varchar(100) ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10, 20, 60, 80'); Query OK, ... Read More

Advertisements