How To Use Mysql Distinct To Eliminate Duplicate Rows
How To Use Mysql Distinct To Eliminate Duplicate Rows
Summary: In this tutorial, you will learn how to use MySQL DISTINCT with SELECT statement to eliminate duplicate records in the selected result set. Sometimes when retrieving data from database table, you get duplicate records which are not expected. In order to remove those duplicate records, you need to use DISTINCT keyword along with SELECT statement. The syntax of SQL DISTINCT is as follows:
The MySQL LIMIT also allows you to get a range of records where you decide starting record number and how many records you want to retrieve. Here is the syntax of MySQL LIMIT to select a range of records:
1 SELECT columns
In the query above, S is the starting record index. MySQL specifies that the first record starts with 0. N is the number of records you want to select. Let's practice MySQL LIMIT with several examples to have a better understanding. If you want to get the first five employees in the table employees, you can use the following query: view source print?
1 SELECT firstname,lastname 2 FROM employees 3 LIMIT 5
Now if you want to get five employees from employee number 10 you can use MySQL LIMIT with offset as follows: view source print?
1 SELECT firstname,lastname 2 FROM employees 3 LIMIT 10,5
The column in WHERE clause does not need to be in column_list you selected, but it has to be a column in the table table_name. If the list has more than one value, each item has to be separated by a comma. In addition, you can use NOT operator with SQL IN to get values which does not match any value in a list of value. Lets practice with several examples of SQL IN. Suppose if you want to find out all offices which are located in US and France, you can perform the following query:
1 SELECT officeCode, city, phone 2 FROM offices 3 WHERE country = 'USA' OR country = 'France'
To get all countries which does are not located in USA and France, we can use NOT IN in the where clause as follows:
1 SELECT officeCode, city, phone 2 FROM offices 3 WHERE country NOT IN ('USA','France')
Here is the output of offices which does not in USA and France
+------------+--------+------------------+ | officeCode | city | phone | +------------+--------+------------------+ | 5 | Tokyo | +81 33 224 5000 | | 6 | Sydney | +61 2 9264 2451 | | 7 | London | +44 20 7877 2041 | +------------+--------+------------------+
Lets practice with several examples of using SQL BETWEEN to search values in a range. Suppose we want to find all products which buy price is in a range of 90$ and 100$, we can perform the following query to do so:
1 SELECT productCode,ProductName,buyPrice 2 FROM products 3 WHERE buyPrice BETWEEN 90 AND 100 4 ORDER BY buyPrice DESC
| S12_1108 | 2001 Ferrari Enzo | S12_1099 | 1968 Ford Mustang | S18_1984 | 1995 Honda Civic | S18_4027 | 1970 Triumph Spitfire | S10_4698 | 2003 Harley-Davidson Eagle Drag Bike +-------------+--------------
| | | | |
| | | | |
Suppose you want to search for employee in employees table who has first name starting with character a, you can do it as follows: view source print?
1 SELECT employeeNumber, lastName, firstName 2 FROM employees 3 WHERE firstName LIKE 'a%'
Like SQL standard, MySQL UNION allows you to combine two or more result sets from multiple tables together. The syntax of using MySQL UNION is as follows: view source print?
1 SELECT statement 2 UNION [DISTINCT | ALL] 3 SELECT statement 4 UNION [DISTINCT | ALL] 5
In order to use UNION statement, there are some rules you need to follow:
The number of columns in each SELECT statement has to be the same . The data type of the column in the column list of the SELECT statement must be the same or at least convertible.
By default the MySQL UNION removes all duplicate rows from the result set even if you dont explicit using DISTINCT after the keyword UNION. If you use UNION ALL explicitly, the duplicate rows remain in the result set. You only use this in the cases that you want to keep duplicate rows or you are sure that there is no duplicate rows in the result set. Lets practice with couples of examples with MySQL UNION to get a better understanding. Suppose you want to combine customers and employees infomation into one result set, you use the following query:
First, you need to specify the tables you want to join with the main table. The main table appear in the FROM clause. The table you want to join with appear after keyword INNER JOIN. Theoretically, you can join a table with unlimited number of tables. However, for better performance you should limit the number of tables to join based on join conditions and volume of data in those tables. Second, you need to specify the join condition or join predicate. The join condition appears after the keyword ON of MySQL INNER JOIN clause. The join condition is the rule for matching rows between the main table and other tables being joined with.
Lets take a look at two tables: products and orderDetails in our sample database.
The products table is the master data table that stores all products. Whenever a product is sold, it is stored in the orderDetails table with other information. The link between products table and orderDetails table is productCode. Now, if you want to know what product was sold in which order, you can use the MySQL INNER JOIN clause as follows:
1 SELECT A.productCode, A.productName, B.orderNumber 2 FROM products A 3 INNER JOIN orderDetails B on A.productCode = B.productCode;
The MySQL LEFT JOIN clause works like this: when a row from the left table matches a row from the right table based on join_condition, the rows content are selected as an output row. When row in the left table has no match it is still selected for output, but combined with a fake row from the right table that contains NULL in all columns. In short, the MySQL LEFT JOIN clause allows you to select all rows from the left table even there is no match for them in the right table.
1 SELECT c.customerNumber, customerName,orderNUmber, o.status 2 FROM customers c 3 LEFT JOIN orders o ON c.customerNumber = o.customerNumber;
We have orderDetails table in our sample database. We can use the MySQL GROUP BY clause to get all orders, number of items sold and total values in each order as follows:
1 SELECT * 2 FROM table_name 3 ORDER BY column_name ASC LIMIT n (eg:limit n,n-1::::n==start point(5) and n-1(8)==last point we get 4 the record b/w n and n-1.=> means from ascending order it dispaly the record after leaving the first 5 records and count from 6th record upto 8th record)
MySQL provides us the LIMIT clause so we just have to leverage it to rewrite the query as follows:
1 SELECT * 2 FROM table_name
The query just returns the first row after n-1 row(s) so you get the Nth highest record. For example, if you want to get the second most expensive product (n = 2) in Products database table, you just need to perform the following query:
1 SELECT productCode, productName, buyPrice 2 FROM products 3 ORDER BY buyPrice desc 4 LIMIT 1, 1
+-------------+--------------------------------+----------+ | productCode | productName | buyPrice | +-------------+--------------------------------+----------+ | S18_2238 | 1998 Chrysler Plymouth Prowler | 101.51 | +-------------+--------------------------------+----------+ 1 row in set (0.00 sec)
The second technique to get the Nth highest record is using SQL subquery:
1 SELECT * 2 FROM table_name AS a 3 WHERE n - 1 = ( 4 SELECT COUNT(primary_key_column) 5 6 FROM products b WHERE b.column_name > a. column_name)
We can achieve the same result as the first technique on Products table to get the second most expensive product by performing the following query:
1 SELECT productCode, productName, buyPrice 2 FROM products a 3 WHERE 1 = ( 4 SELECT COUNT(productCode) 5 6 FROM products b WHERE b.buyPrice > a.buyPrice)