SELECT TOP 10 firstname, lastname, * FROM [ComplianceAdmin].[dbo].[User] where firstname like 'J%', or id = 38

--this is how we comment for a line.

--so far we have selects, top, from where.

--indent and separate columns with commas.

--remember the different options in the results window.

/* block comments look like this. They go on and on and end

with a close like this */

select db_name()

/* this command sets the db if it is not selected */

USE datamart

--this command tells you the db you are accessing. in SMS Select it in the box over the object exporer.

/* Other things to remeber. SQL commands are not case sensitive. The columns might be. */

/* DISTINCT - the Distinct option after Select gives only the ditinct records. */

select distinct ApplicationName, id from dbo.UserAccess

--or this to count the number of Distincts--

Select count (distinct lastname) from [ComplianceAdmin].[dbo].[User]

--here it is with the Where IN clause, and use NOT IN--

select UserName, * from dbo.UserAccess where Userinit in ('j', 'r');

/* TOP [10] - could be the top of whatever you want - 2, 5, 50, and then

you can follow with PERCENT */

select TOP 10 PERCENT

/* WHERE - the last clause that narrows the selection to

specific conditions on a particular field. Use these =, <, >, <=, >=, <>.

Where can also use LIKE, IN, BETWEEN, CONTAINS.. */

select TOP 10 * from TaxServices.dbo.fund Where FundCode = 'M%'

--here it is with the Where IN clause, and use NOT IN--

select UserName, * from dbo.UserAccess where Userinit in ('j', 'r');

/* To find where a field is NULL you must use IS NULL. Looks like this*/

where ModifiedDate IS Null

-- for a selection between numbers identify the column and the range

select * from dbo.tablename where ID between 4 and 8

/* Use the where clause with "OR" and "AND". The "OR" searches get bigger - more

data, while the "AND" search norrows down yur results. */

/* Use ethe ORDER BY clause in ASC or DESC. Rememeber that the check it you

can run two selects at the same time and the output goes to two windows

in the results pane */

select * from dbo.tablename order by TaxID ASC

select * from dbo.tablename order by TaxID DESC

/* Create and ALIAS TAbles and Field names. YOu can use AS or = to

indicate the ALIAS, or not. */

/* inner Join - this one worked.*/

Select * from dbo.Fund

inner Join dbo.ClientImportSettings

on dbo.Fund.ClientID = dbo.ClientImportSettings.ClientID

--returning some usefull data..

Select Getdate ()

select db_name ()

select 'Screw U'

/* USE statement changes db in entire window. Specifying db name in 1 query affects that query only.*/