Apply Filtering Criteria at Group Levels in MySQL

Arjun Thakur
Updated on 20-Jun-2020 06:39:36

148 Views

As we know that GROUP BY clause in a SELECT statement can divide the result set, returned by MySQL, in groups. Now if we want to return only some specific groups then need to apply filtering criteria at the group level. It can be done by using HAVING clause inside the GROUP BY clause. The example below will demonstrate it −ExampleSuppose we want to return only the group which is having an average salary of 55000 then we need to use filtering criteria as follows in HAVING clause −mysql> Select count(*), AVG(salary), Designation from employees GROUP BY designation having AVG(salary) ... Read More

Divide MySQL Result Set into Groups

Sai Nath
Updated on 20-Jun-2020 06:36:49

1K+ Views

It can be done by using GROUP BY clause in the SELECT statement. We can specify a column as grouping criteria with the help of GROUP BY clause. Due to the specified grouping criteria, rows with the same value in a particular column are considered as a single group. In this way, the result set returned by MySQL SELECT statement will be divided into groups.ExampleFollowing is a good example to understand it −We have a table named ‘employees’ as follows −mysql> Select * from employees; +------+-------------+--------+------------+ | id   | designation | Salary | DoJ        | +------+-------------+--------+------------+ ... Read More

Convert Unix Timestamp to MySQL Timestamp Value

Nikitha N
Updated on 20-Jun-2020 06:35:33

2K+ Views

MySQL converts Unix timestamp to timestamp data type value with the help of  FROM_UNIXTIME() function.Examplemysql> Select FROM_UNIXTIME(1508622563); +-----------------------------+ | FROM_UNIXTIME(1508622563)   | +-----------------------------+ | 2017-10-22 03:19:23         | +-----------------------------+ 1 row in set (0.00 sec)

Out of Range Value in UNIX_TIMESTAMP or FROM_UNIXTIME Function in MySQL

varun
Updated on 20-Jun-2020 06:35:01

376 Views

When we pass an out-of-range value in UNIX_TIMESTAMP, MySQL returns 0. The valid range of value is same as for the TIMESTAMP data type.Examplemysql> Select UNIX_TIMESTAMP('1969-01-01 04:05:45'); +---------------------------------------+ | UNIX_TIMESTAMP('1969-01-01 04:05:45') | +---------------------------------------+ |                         0             | +---------------------------------------+ 1 row in set (0.00 sec)When we pass an out-of-range value in FROM_UNIXTIME, MySQL returns NULL. The valid range of values is same as for the INTEGER data type.Examplemysql> Select FROM_UNIXTIME(2147483648); +---------------------------+ | FROM_UNIXTIME(2147483648) | +---------------------------+ | NULL                      | +---------------------------+ 1 row in set (0.00 sec)

MySQL Microseconds in Timestamp Value Conversion to Integer

seetha
Updated on 20-Jun-2020 06:34:20

125 Views

As we know that the value of timestamp can be converted to a number of seconds with the help of UNIX_TIMESTAMP() function. MySQL would ignore the microseconds added to the value of timestamp because the value of UNIX_TIMESTAMP is only 10digits long.Examplemysql> SELECT UNIX_TIMESTAMP('2017-10-22 04:05:36')AS 'Total Number of Seconds'; +-------------------------+ | Total Number of Seconds | +-------------------------+ | 1508625336              | +-------------------------+ 1 row in set (0.00 sec) mysql> SELECT UNIX_TIMESTAMP('2017-10-22 04:05:36.200000')AS 'Total Number of Seconds'; +-------------------------+ | Total Number of Seconds | +-------------------------+ | 1508625336              | +-------------------------+ ... Read More

Use Group Functions with Non-Group Fields in MySQL SELECT Query

Swarali Sree
Updated on 20-Jun-2020 06:33:44

250 Views

We have to use GROUP BY clause if we want to use group functions with non-group fields in the SELECT query. The general syntax can be as followsSyntaxSELECT group_function1, …, non-group-column1, … from table_name GROUP BY column_name;Examplemysql> Select COUNT(*), id from Student GROUP BY id; +----------+------+ | COUNT(*) | id   | +----------+------+ | 1        | 1    | | 1        | 2    | | 1        | 15   | | 1        | 17   | | 1        | 20   | +----------+------+ 5 ... Read More

Filter Duplications in MySQL Result Set

Ankith Reddy
Updated on 20-Jun-2020 06:30:30

117 Views

It can be possible by using the DISTINCT keyword in SELECT clause. The DISTINCT applies to the combination of all data fields specified in SELECT clause.ExampleWe have the table ‘Student’ on which we have applied DISTINCT keyword as follows −mysql> Select * from student; +------+---------+---------+-----------+ | Id   | Name    | Address | Subject   | +------+---------+---------+-----------+ | 1    | Gaurav  | Delhi   | Computers | | 2    | Aarav   | Mumbai  | History   | | 15   | Harshit | Delhi   | Commerce  | | 17   | Raman   | ... Read More

Difference Between UNIX TIMESTAMPS and MySQL TIMESTAMPS

vanithasree
Updated on 20-Jun-2020 06:29:55

2K+ Views

In MySQL, UNIX TIMESTAMPS are stored as 32-bit integers. On the other hand MySQL TIMESTAMPS are also stored in similar manner but represented in readable YYYY-MM-DD HH:MM:SS format.Examplemysql> Select UNIX_TIMESTAMP('2017-09-25 02:05:45') AS 'UNIXTIMESTAMP VALUE'; +---------------------+ | UNIXTIMESTAMP VALUE | +---------------------+ | 1506285345          | +---------------------+ 1 row in set (0.00 sec)The query above shows that UNIX TIMESTAMPS values are stored as 32 bit integers whose range is same as MySQL INTEGER data type range.mysql> Select FROM_UNIXTIME(1506283345) AS 'MySQLTIMESTAMP VALUE'; +----------------------+ | MySQLTIMESTAMP VALUE | +----------------------+ | 2017-09-25 01:32:25  | +----------------------+ 1 row in set (0.00 sec)The query ... Read More

How GROUPS Function Can Be Used in MySQL SELECT Clause

Alankritha Ammu
Updated on 20-Jun-2020 06:29:28

159 Views

As we know that group function operates on the sets of value that is why if group functions will be used in SELECT clause then they will be used on the rows that meet the query selection criteria and the output of group functions will be returned as the output of the query.ExampleIn the example below, we have used some group functions in a SELECT statement on the fields of ‘Student’ table and the output of the statement is the output of those group functions −mysql> Select COUNT(Name), MIN(Id), AVG(Id), MAX(Id), COUNT(*) from Student; +-------------+---------+---------+---------+----------+ | COUNT(Name) | MIN(Id) | ... Read More

Count Rows in a Table Using MySQL SELECT Statement

Paul Richard
Updated on 20-Jun-2020 06:29:06

253 Views

We need to use COUNT(*) function with SELECT clause to count the total number of rows in a table.Examplemysql> Select COUNT(*) from Student; +----------+ | COUNT(*) | +----------+ | 4        | +----------+ 1 row in set (0.06 sec)The query above counts the total number of rows of ‘Student’ table.We can also use WHERE clause with COUNT(*) function as follows:mysql> Select COUNT(*) from Student where Address = 'Delhi'; +----------+ | COUNT(*) | +----------+ | 2        | +----------+ 1 row in set (0.00 sec)

Advertisements