Answers for "select median from sql"

SQL
2

find the median in sql

# FOR TABLES WITH ODD NUMBER OF ROWS

# For column1 in table1 with 'n' number of rows. Where 'n' is an odd number.
# 1. Change all column1 and table1 to your column and table name.
# 2. Calculate (n/2)+0.5, where n=number of rows, and set it as LIMIT for t1.

SELECT *
FROM (SELECT column1
      FROM table1
      ORDER BY column1
      LIMIT (n/2)+0.5) AS t1
ORDER BY column1 DESC
LIMIT 1;
Posted by: Guest on July-28-2021
2

find the median in sql

# FOR TABLES WITH EVEN NUMBER OF ROWS

# For column1 in table1 with 'n' number of rows. Where 'n' is an even number.
# 1. Change all column1 and table1 to your column and table name.
# 2. Calculate (n/2)+1, where n=number of rows, and set it as LIMIT for t1.

SELECT AVG(t2.column1)
FROM (SELECT *
	FROM (SELECT column1
      	FROM table1
     	 ORDER BY column1
     	 LIMIT (n/2)+1) AS t1
	ORDER BY column1 DESC
	LIMIT 2) AS t2;
Posted by: Guest on July-28-2021

Code answers related to "SQL"

Browse Popular Code Answers by Language