Sunday, May 17, 2015

Write a SQL Program to get the Sum value of same column with different conditions.

CREATE TABLE student(id int, name varchar(30), age int, gender char(6));

INSERT INTO student VALUES
(1 ,'Ranga', 27, 'Male'),
(2 ,'Reddy', 26, 'Male'),
(3 ,'Vasu', 50, 'Female'),
(5 ,'Raja', 10, 'Male'),
(6 ,'Pavi', 52, 'Female'),
(7 ,'Vinod', 27, 'Male');

Query:
-----------------------------------
SELECT SUM(CASE WHEN s.gender = 'Male' THEN 1 ELSE 0 END) AS MaleCount,
SUM(CASE WHEN s.gender = 'Female' THEN 1 ELSE 0 END) AS FemaleCount
FROM
student s;

Output:
------------------------------
MaleCount FemaleCount
4 2

Related Posts:

  • Method References in Java8 Method References: Method references refers to methods or constructors without invoking them. We can use lambda expressions to create anonymous methods.  Sometimes, however, a lambda expression d… Read More
  • Difference Between ClassNotFoundException and NoClassDefFoundErrorClassNotFoundException (java.lang.ClassNotFoundException): ClassNotFoundException is a checked Exception derived directly from java.lang.Exception class.public class ClassNotFoundException extends Exc… Read More
  • Lambda Expressions in Java 8 Lambda Expressions: Lambda expression is an anonymous function without any declarations.  Lambda Expression are useful to write shorthand code and hence saves the effort of writing lengthy code.  It promotes d… Read More
  • Javascript code to compare two datesIn this post, we are going to see how to compare two dates in Java Script.<html> <head> <title>Date Difference</title> <script> function CheckDate() { … Read More
  • Difference between Abstraction and EncapsulationIn Java both Abstraction and Encapsulation are two important OOP (Object Oriented Programming) concepts or principles. Both are completely different from each other. Abstraction(Hiding): Abstraction means hiding.Abstra… Read More

0 comments: