T-SQL Basic Query Writing
T-SQL Basic Query Writing
SQL Server
These are the basic topics focusing around the beginning aspects of using SQL Server. Topics are
kept general as to apply to most RDBMS’ similar to SQL Server
SQL Server Basics
SQL Server Languages – DML, DDL, DQL, DCL
Update TableA
Set Column3 = ‘Univierse’
Where Column3 = ‘World’
Delete
From TableA
Where Column1 = 1
Truncate – Remove ALL rows of data from a table (May be considered DDL by some)
Truncate Table
TableA
Drop table
TableA
Drop database
Adventureworks2012
Page 1 of 6
SQL Server Basics
DQL – Data Query Language – Select, From, Where, Group By, Having, Order By
From dbo.TableA
Where Column1 = 1
Group By Column1
Grant Select
On dbo.TableA
To Jane
Revoke Select
On dbo.TableA
From Jane
Deny Select
On dbo.TableA
To Jane
Page 2 of 6
SQL Server Basics
SQL Server Constraints
Primary Key – Also uses Not Null and Unique, this specifies a column(s) to be used in identifying
each individual row of data. It will create a Unique Clustered Index as a result.
Foreign Key – Used to specify that the chosen column will be referencing another column set of
data in table.
Unique Key – Used to specify that a column(s) should be indexed and sorted for faster retrieval
of data. Used on columns that are commonly queried. Creates Unique non clustered index.
Check – Used to limit what values will be valid within the column.
Not Null – Specify if Null values are allowed within a column or not
Dropping a Constraint
All constraints except Null and Not Null can be removed. A column must always be either Null
or Not Null
Page 3 of 6
SQL Server Basics
Joining Tables in SQL
Tables are often divided into smaller pieces through the process of Normalization. This helps to limit
data to only what is needed at any given time. The less data you pull, the faster the process. Sometimes
though, we need more information and must combine tables together. This is often done using Joins.
Inner Join – This will combine rows from multiple tables, where the column values must match
Select *
From TableA
Inner join TableB
On TableA.Column1 = TableB.Column1
Left Outer Join – This will display the matching values between the two tables, just like inner
join, but ALSO display the non-matching values from the first table
Select *
From TableA
Left Outer join TableB
On TableA.Column1 = TableB.Column1
Right Outer Join – This will display the matching values between the two tables, just like inner
join, but ALSO display the non-matching values from the second table
Select *
From TableA
Right Outer join TableB
On TableA.Column1 = TableB.Column1
Full Outer Join – This join will display all values from both tables, whether they match or do not
match
Select *
From TableA
Full Outer join TableB
On TableA.Column1 = TableB.Column1
Page 4 of 6
SQL Server Basics
Data Modeling in SQL Server
Data modeling is essential in the creation of any database. Here, you’ll design a blue print to the overall
design of the entire database. Each individual table, column, and relationship would be displayed. To do
this we use Entity Relationship Diagrams like below.
In this ER-Diagram, each table is known as an Entity. They are represented with the Rectangles
The columns that would be found in those tables are displayed as attributes, with ovals
This diagram is in a simple form, and displaying only the Entities and Attributes. There are no direct
references to the type of relationships that are being used.
Relationships are how entities relate or connect their data to one another. Relationships will often be
displayed using a diamond shape, as seen in the next figure.
Page 5 of 6
SQL Server Basics
In this more detailed ER-Diagram, we can see the relationships, their description, and how many
instances for each entity.
With the descriptions, you see them in Diamond shapes, showing what action is being taken within the
relationship. This just helps to understand the nature of the relation and how the data connects
In small annotations, you’ll see [1:1], [1:M], and sometimes [M:N]. These are the instances or cardinality
of how many times an entity is participating in the relationship. Most commonly you’ll have either one-
to-one, one-to-many, or many-to-many.
Page 6 of 6