Search
 
SCRIPT & CODE EXAMPLE
 

SQL

HOW TO FIND MEDIAN IN SQL FOR BOTH IDD AND EVEN

/*
HACKER RANK MYSQL SOLUTION.
*/
SET @rowindex := -1;
 
SELECT
   ROUND(AVG(N.LAT_N),4)
FROM
   (SELECT @rowindex:=@rowindex + 1 AS rowindex,
           STATION.LAT_N AS LAT_N
    FROM STATION
    ORDER BY STATION.LAT_N) AS N
WHERE
N.rowindex IN (FLOOR(@rowindex / 2) , CEIL(@rowindex / 2));
Comment

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;
Comment

how to find median of a column sql

SET @rowindex := -1;
 
SELECT
   AVG(g.grade)
FROM
   (SELECT @rowindex:=@rowindex + 1 AS rowindex,
           grades.grade AS grade
    FROM grades
    ORDER BY grades.grade) AS g
WHERE
g.rowindex IN (FLOOR(@rowindex / 2) , CEIL(@rowindex / 2));
Comment

PREVIOUS NEXT
Code Example
Sql :: oracle get running queries 
Sql :: how to generate a unique random number in mysql 
Sql :: drop primary key oracle 
Sql :: foreign key constraint in ms sql 
Sql :: update table disable constraint 
Sql :: oracle apex warn on unsaved changes 
Sql :: sql check duplicate value in column 
Sql :: how to insert json value in mysql 
Sql :: copy value from one column to another postgres 
Sql :: rename table sql server 
Sql :: mysql shell clear screen 
Sql :: add colum date in sql 
Sql :: postgres select duplicate columns 
Sql :: rename table name 
Sql :: oracle temporary table 
Sql :: mysql decimal allow negative values? 
Sql :: wherein sql 
Sql :: postgresql not case sensitive where in 
Sql :: sql query for getting data with join and count 
Sql :: oracle partition table row count 
Sql :: reset auto increment mysql 
Sql :: how to drop database name in postgresql 
Sql :: convert polygon to text in mysql 
Sql :: mysql order by 
Sql :: mysql auerries to find the name starting with vowel letter 
Sql :: oracle alter table add column default value 
Sql :: unique key in ms sql server 
Sql :: subquery in sql 
Sql :: how insert auto increment 
Sql :: sqlite3 update select 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =