DEV Community

vidhya murali
vidhya murali

Posted on

The SELECT DISTINCT Statement

Day -5

The SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values and sometimes you only want to list the different (distinct) values.

CREATE TABLE employees (
    emp_id INT,
    name VARCHAR(50),
    city VARCHAR(50),
    department VARCHAR(50),
    salary INT
);
Enter fullscreen mode Exit fullscreen mode
INSERT INTO employees VALUES
(101, 'Vidhya', 'Chennai', 'IT', 35000),
(102, 'Anu', 'Chennai', 'HR', 30000),
(103, 'Ravi', 'Bangalore', 'IT', 40000),
(104, 'Kumar', 'Chennai', 'Finance', 45000),
(105, 'Priya', 'Bangalore', 'HR', 32000),
(106, 'Arun', 'Mumbai', 'IT', 38000),
(107, 'Divya', 'Mumbai', 'Finance', 42000),
(108, 'Siva', 'Chennai', 'IT', 35000);
Enter fullscreen mode Exit fullscreen mode
  1. Display all unique cities

  2. Display all unique departments.

  3. Display unique salaries.

  4. Display unique combinations of city and department

select distinct city 
from empolyees

select distinct department 
from empolyees

select distinct salary 
from empolyees

select distinct department,city
from empolyees
Enter fullscreen mode Exit fullscreen mode
  1. Display unique cities where salary is greater than 35000.

  2. Display unique departments where salary is greater than 30000.

  3. Display unique cities of employees working in the IT department.

  4. Display unique departments of employees working in Chennai.

select distinct city 
from employees_india
where salary>35000

select distinct department 
from employees_india
where salary>30000

select distinct city 
from employees_india
where department='IT'

select distinct department 
from employees_india
where city='Chennai'

Enter fullscreen mode Exit fullscreen mode
  1. Display unique combinations of city and department for employees whose salary is greater than 35000.

  2. Display unique cities where employees belong to either IT or Finance.

  3. Display unique departments where employees are from Chennai or Mumbai.

  4. Display unique salary values for employees working in IT.

select distinct city,department 
from employees_india
where salary>35000

select distinct city 
from employees_india
where department in ('IT','Finance');

select distinct department 
from employees_india
where city in ('chennai','Mumbai);

select distinct salary 
from employees_india
where department='IT';
Enter fullscreen mode Exit fullscreen mode

Filter Records

The WHERE **clause **is used to filter records.

It is used to extract only those records that fulfill a specified condition.

Top comments (1)

Collapse
 
kamalesh_ar_6252544786997 profile image
Kamaleshwar A

Good! easily understandable.👍🏻