PHP Tutorials
- What is MySqli
- mysql query
- mysql query example
- InnoDB
- mysql column Type
- CRUD Example
- Connection Using Function
- mysql keys
- SELECT
- WHERE
- UPDATE
- Count no of Rows
- ALIAS
- AND, AND & OR
- BETWEEN
- COMPARISON OPERATOR
- DELETE
- DELETE LIMIT
- DISTINCT
- EXISTS
- FROM
- GROUP BY
- HAVING
- IN
- INTERSECT
- IS NULL & IS NOT NULL
- LIKE
- NOT
- ORDER BY
- SELECT LIMIT
- SUBQUERY
- TRUNCATE
- UNION && UNION ALL
- Concat & Group_Concat
- mysql Function
- Mysql Insert Id
- MySql Aggregate Function
- Mysql Join
- JOIN in MySql
- Trigger
- Procedure
- Transaction
- views
- Index
- SQL Injection
- Normalization
- Query Bind
- Interview Questions
Important Link
What is DISTINCT in mysql?
DISTINCT is used to remove duplicates rows from table. DISTINCT fetch only the unique records. The DISTINCT is used with SELECT statements.
Syntax :-
SELECT DISTINCT
list_of_column_name
FROM
table_name
WHERE
search_condition
ORDER BY
expression;
With Single Expression
if you specify single column in your distinct query then it return unique rows based on the condition of that column. and it removes the duplicates from the result set.
SELECT DISTINCT NAME FROM ITTUTORIAL;
Example :-
SELECT DISTINCT city FROM state where state = 'bihar'
SELECT DISTINCT(city) FROM state where state = 'bihar'
Example - With Multiple column
We can also combine 2 or multiple columns to get distinct values.
it return unique record, department and salary basis.
SELECT distinct Department, Salary FROM employee;
Example - With Multiple Expressions
SELECT DISTINCT city, state
FROM customers;
Example
SELECT DISTINCT fnane FROM itechxpert ORDER BY fname;
DISTINCT and NULL values
When you specify a column that has NULL values in the DISTINCT clause, the DISTINCT clause will keep only one NULL value because it considers all NULL values are the same.
File Name :
MySQL DISTINCT with multiple columns
SELECT DISTINCT
state, city
FROM
customers
WHERE
state IS NOT NULL
ORDER BY
state,
city;
output
state | city |
+---------------+----------------+
| BC | Tsawassen |
| BC | Vancouver |
| CA | Brisbane |
| CA | Burbank |
| CA | Burlingame |
| CA | Glendale |
| CA | Los Angeles |
| CA | Pasadena |
| CA | San Diego
SELECT COUNT(DISTINCT Country) FROM ittutorial;