Tuesday, 2 August 2016

AVG function



The AVG() function returns the average value of a numeric column.

CREATE TABLE employee(
  id int PRIMARY KEY,
  name VARCHAR(25),
  age int,
  salary DECIMAL(10, 4),
  mailId VARCHAR(25),
  city VARCHAR(10)
);

INSERT INTO employee VALUES (1, "Hari", 28, 12345.67, "hari@hari.com", "Bangalore");
INSERT INTO employee VALUES (2, "Sandesh", 31, 98345.00, "sandesh@sandesh.com", "Trivendram");
INSERT INTO employee VALUES (3, "Phalgum", 33, 119345.67, "phalgun@hari.com", "Hyderabad");
INSERT INTO employee VALUES (4, "Manju", 36, 87666.87, "manju@sandesh.com", "Bangalore");
INSERT INTO employee VALUES (5, "Rakesh", 26, 38000, "rakesh@hari.com", "Bangalore");
INSERT INTO employee VALUES (6, "Sankalp", 38, 87645.67, "sankalp@sankalp.com", "Chenai");
INSERT INTO employee VALUES (7, "Vadiraj", 40, 12345.67, "vadi@hari.com", "Bangalore");
INSERT INTO employee VALUES (8, "Prasob", 37, 12345.67, "prasob@sandesh.com", "Trivendram");
INSERT INTO employee (id, name, salary) VALUES (9, "Kesav", 123457.89);

CREATE TABLE employee_info(
  id int PRIMARY KEY,
  city VARCHAR(20),
  state VARCHAR(20),
  country VARCHAR(20)
);

INSERT INTO employee_info VALUES (1, "Bangalore", "Karnataka", "India");
INSERT INTO employee_info VALUES (2, "Chirala", "Andhra Pradesh", "India");
INSERT INTO employee_info VALUES (3, "Punuru", "Andhra Pradesh", "India");
INSERT INTO employee_info VALUES (4, "Bangalore", "Karnataka", "India");
INSERT INTO employee_info VALUES (5, "Bangalore", "Karnataka", "India");
INSERT INTO employee_info VALUES (6, "Nellore", "Andhra Pradesh", "India");
INSERT INTO employee_info VALUES (7, "Chennai", "Tamilnadu", "India");
INSERT INTO employee_info VALUES (17, "Bhuvaneswar", "Orissa", "India");
INSERT INTO employee_info VALUES (27, "Mumbai", "Maharastra", "India");
INSERT INTO employee_info VALUES (37, "Delhi", "Delhi", "India");

mysql> SELECT * FROM employee;
+----+--------------+------------+------------+
| id | firstName    | lastName   | salary     |
+----+--------------+------------+------------+
|  1 | Hari Krishna | Gurram     |   12345.67 |
|  2 | Rama Devi    | Gurram     | 1234578.67 |
|  3 | Lakshmana    | Rao        | 9876543.67 |
|  4 | Rama         | Krishna    | 1234587.67 |
|  5 | Sowmya       | asd        | 1238745.67 |
|  6 | Jyotsna      | PS         |   76543.67 |
|  7 | Gireesh      | Amara      |   87698.00 |
|  8 | Sravani      | Nidamanuri |  987654.00 |
|  9 | Saranya      | Amara      | 1987654.00 |
+----+--------------+------------+------------+
9 rows in set (0.00 sec)

mysql> SELECT * FROM employee_info;
+----+-------------+----------------+---------+
| id | city        | state          | country |
+----+-------------+----------------+---------+
|  1 | Bangalore   | Karnataka      | India   |
|  2 | Chirala     | Andhra Pradesh | India   |
|  3 | Punuru      | Andhra Pradesh | India   |
|  4 | Bangalore   | Karnataka      | India   |
|  5 | Bangalore   | Karnataka      | India   |
|  6 | Nellore     | Andhra Pradesh | India   |
|  7 | Chennai     | Tamilnadu      | India   |
| 17 | Bhuvaneswar | Orissa         | India   |
| 27 | Mumbai      | Maharastra     | India   |
| 37 | Delhi       | Delhi          | India   |
+----+-------------+----------------+---------+
10 rows in set (0.00 sec)


Get average salary of all employees

mysql> SELECT AVG(salary) FROM employee;
+----------------+
| AVG(salary)    |
+----------------+
| 1859594.557778 |
+----------------+
1 row in set (0.01 sec)


Get average salary of employees city wise

mysql> SELECT empInfo.city, AVG(emp.salary) FROM employee emp, employee_info empInfo WHERE emp.id=empInfo.id GROUP BY empInfo.city;
+-----------+-----------------+
| city      | AVG(emp.salary) |
+-----------+-----------------+
| Bangalore |   828559.670000 |
| Chennai   |    87698.000000 |
| Chirala   |  1234578.670000 |
| Nellore   |    76543.670000 |
| Punuru    |  9876543.670000 |
+-----------+-----------------+
5 rows in set (0.01 sec)


Get average salary of employees city wise and sort them by average salary.

mysql> SELECT empInfo.city, AVG(emp.salary) AS employeeSalary FROM employee emp, employee_info empInfo WHERE emp.id=empInfo.id GROUP BY empInfo.city ORDER BY employeeSalary;
+-----------+----------------+
| city      | employeeSalary |
+-----------+----------------+
| Nellore   |   76543.670000 |
| Chennai   |   87698.000000 |
| Bangalore |  828559.670000 |
| Chirala   | 1234578.670000 |
| Punuru    | 9876543.670000 |
+-----------+----------------+
5 rows in set (0.00 sec)






Previous                                                 Next                                                 Home

Monday, 1 August 2016

Sort elements of a list in predefined order

Suppose you are maintaining list of languages like below.

English
Hindi
Telugu
Sanskrit
Chinese
Japanese
French


Whatever the languages order you are getting, your sort function should sort them like the order given above. By using map kind of data structure and custom comparator, you can sort the elements in custom order.
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
 private static Map<String, Integer> map = new HashMap<>();

 static {

  map.put("English", 1);
  map.put("Hindi", 2);
  map.put("Telugu", 3);
  map.put("Sanskrit", 4);
  map.put("Chinese", 5);
  map.put("Japanese", 6);
  map.put("French", 7);

 }

 public static List<String> getSortedData(List<String> list) {
  Collections.sort(list, new Comparator<String>() {
   @Override
   public int compare(String str1, String str2) {
    return map.get(str1) - map.get(str2);
   }
  });
  return list;
 }

 public static void main(String args[]) {
  List<String> list1 = Arrays.asList("English", "Hindi", "Telugu", "Sanskrit", "Chinese", "Japanese", "French");

  List<String> list2 = Arrays.asList("Chinese", "Japanese", "French", "English", "Hindi", "Telugu", "Sanskrit");

  List<String> list3 = Arrays.asList("English", "Hindi", "Chinese", "Japanese", "French", "Telugu", "Sanskrit");

  System.out.println(getSortedData(list1));
  System.out.println(getSortedData(list2));
  System.out.println(getSortedData(list3));

 }
}


Output
[English, Hindi, Telugu, Sanskrit, Chinese, Japanese, French]
[English, Hindi, Telugu, Sanskrit, Chinese, Japanese, French]
[English, Hindi, Telugu, Sanskrit, Chinese, Japanese, French]

Is System.exit takes negative value?


Yes, System.exit takes –ve value, As you see the documentation of System .exit, the signature looks like below.
public static void exit(int status);

public class Test {
 public static void main(String ptr[]) {
  System.out.println("I am displaying");
  System.exit(-100);
  System.out.println("I am not displaying");
 }
}