Thursday, May 31, 2012

SQL SELECT DISTINCT Statement


In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table.

The DISTINCT keyword can be used to return only distinct (different) values.


SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name (s) FROM table

SQL SELECT Statement


The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.

SQL SELECT Syntax

SELECT column_name (s)  FROM table_name    --To select specific columns only from a table


SELECT * FROM table_name    --To list the table


SQL Query to Find Column From All Tables of Database

USE [xxx] --Provide DB Name
GO
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE 'xxxx'    --Provide Column name--
ORDER BY schema_name, table_name;