Free Newsletters:
DatabaseDaily  
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed


follow us on Twitter





Brocade Doubles Down on 16 Gbps Fibre Channel

Microsoft Wants iOS Apps to Run on WP7

Avaya Debuts New Virtual Services Switch
Database Journal |DBA Support |SQLCourse |SQLCourse2







Technical Specialist – Pre-sales (MA)
Next Step Systems
US-MA-Littleton

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

SQL etc

August 18, 2000

Simple SQL: Pt. 1 - Page 3

By Ted Brockwood

Getting Acquainted with DISTINCT

An excellent keyword to get acquainted with is "DISTINCT". If you have data that is duplicated, but you only want to see one instance of it, DISTINCT is just the ticket.

For example, you have a table named CUSTOMERS. It is loaded with the names, addresses, phone numbers, and company names of all your best customers. Your boss needs to know what companies have been buying from you. Frequently you have multiple buyers from the same company, perhaps different divisions, so running a standard SELECT statement like this:

SELECT FIRST_NAME, LAST_NAME, COMPANY
       FROM CUSTOMERS;

will return something like this:

FIRST_NAMELAST_NAMECOMPANY
JoeJonesIsoLunacy, Inc
DonnaSprintMegaData Systems Corp
WillBrownIsoLunacy, Inc

As is obvious, you have two customers from IsoLunacy, Inc in your query results. If this were thousands of duplicates, it would be a terrible headache to create a list manually of companies you deal with. So, you fire off a DISTINCT query such as this:

SELECT DISTINCT COMPANY FROM CUSTOMERS;

Which returns:

COMPANY
IsoLunacy, Inc.
MegaData Systems Corp

SELECT statements are also adept at handling comparisons such as greater than and less than. Imagine the table "EMPLOYEES" with Names, Addresses, Start dates, vacation days accrued, and vacation days taken contained within it. The boss wants to know (for the Human Resources Intranet) whether anyone has taken more vacation days than they have accrued. Your SELECT might look like this:

SELECT FIRST_NAME,LAST_NAME FROM EMPLOYEES
       WHERE VACATION_TAKEN > VACATION_ACCRUED;

The results might look like:

FIRST_NAMELAST_NAME
JoeSmith

Good information for HR, and bad information for the employee names resulting from this query.

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

SQL etc Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 



Latest Forum Threads
SQL etc Forum
Topic By Replies Updated
get records within same group and with condition jutiyi 3 January 12th, 03:07 PM
Database SQL help needed to check date range clashes! deadlydragon121 7 January 9th, 02:47 PM
Remove Alpha from data in Column disk244 0 November 19th, 11:13 AM
sql query releatıon record show and sub total and grand total thank you for your howerlover 0 November 18th, 01:55 AM