You are on page 1of 3

Programming SQL Server 2016

Working with Tables

Topics Homework
 Table creation 1. Create a table that has two column
 Basic syntax Column 1: auto incrementing integer value, SQL
 Data type Server to generate automatically
 Null values Column 2: GUID value, SQL Server to generate
 Identity column automatically
 Unique identifier 2. Insert 5 rows of data into the table
 Sequence – new in SQL Server 2012 3. Find out the last values inserted into the column 1
4. You want to insert a row with value 250 in Column 1.
Show how you do it.
5. Show me all the rows in the table Insert some data

Important Points


Summary


1
SELECT TOP n PERCENT column-list FROM table-name
DML (Data Manipulation Language) → returns top n% rows of the result-set
 DML allows retrieving, storing, modifying or deleting data → use whole number in place of n
 There are four basic DML statements SELECT TOP 3 CustomerID, Country from customers
→ SELECT – to retrieve data from table GO
→ INSERT – to store data into a table SLECT TOP 3 PERCENT CustomerID, Country
→ UPDATE – to modify data in a table FROM customers
→ DELETE – to delete data from a table GO
SELECT Statement  Output
CustomerID Country
 Retrieves rows from the database and enables the
selection of one or many rows or columns from one or ALFKI Germany
many tables. ANATR Mexico
 The main clauses can be summarized as: ANTON Mexico
SELECT select-list FROM table_source
WHERE search-condition CustomerID Country
GROUP BY group-by-expression ALFKI Germany
HAVING search-condition ANATR Mexico
ORDER BY order-expression ANTON Mexico
→ Only SELECT clause is always required and other clauses
you add for various purposes
Sorting Query Results
→ The order of the clauses is significant. When the optional  To sort ORDER BY Clause is used
clauses are used, they must appear in the appropriate  Syntax is:
order. SELECT column-list FROM table-name
ORDER BY column ASC|DESC
The “USE” Statement  ASC for ascending order, DESC from descending order
 Tables hold data, tables are contained in database. SELECT CustomerID, CompanyName, Country, City
 So to retrieve data from a table, you have to set your FROM Customers
current working database appropriate one ORDER BY Country
 The "USE" statement sets the current database GO
 In our lessons, we will use Northwind database  Output
 So the first statement, we have to run is: CustomerID CompanyName Country City
USE Northwind
CACTU Cactus.. Argentina Buenos Aires
GO
 Start writing some SELECT OCEAN Océan.. Argentina Buenos Aires
 Let’s try some simple SELECT query RANCH Ranch.. Argentina Buenos Aires
 For more most of the examples we will use Northwind ERNSH Ernst H.. Austria Graz
sample database PICCO Piccol.. Austria Salzburg
 If you do not have Northwind database, you have to .. … … ..
install it.  For Descending Sorting use DESC option
 In attachments there is a file “insnwind2k12.sql” file SELECT CustomerID, CompanyName, Country, City
 Open the in Query Editor of SQL Management studio, FROM Customers
execute the whole script. ORDER BY CustomerID DESC
 Simple Select GO
 Select all rows including all columns:  Output
SELECT * FROM customers CustomerID CompanyName Country City
GO WOLZA Wolski Zajazd Poland Warszawa
 Output
CustomerID CompanyName .. Phone Fax WILMK Wilman Kala Finland Helsinki
WHITC White Clover USA Seattle
ALFKI Alfreds Futterkiste .. 030-0.. 030-0076545
Markets
ANATR Ana Trujillo .. .. (5) 55.. (5) 555-3745 .. … … ..
.. .. .. .. ..
 You can set sort order on multiple column
 The statement returns all rows and includes all columns SELECT CustomerID, CompanyName, Country, City
→ * is a wild card, that stands for all columns FROM Customers
→ The columns are shown in natural order - as they are in ORDER BY Country ASC, City DESC
the table GO
 Selecting with column list CustomerID CompanyName Country City
SELECT CustomerID, CompanyName, Country, City CACTU Cactus Comidas.. Argentina Buenos Aires
FROM Customers OCEAN Océano Atlántico.. Argentina Buenos Aires
GO RANCH Rancho grande Argentina Buenos Aires
 Output PICCO Piccolo und mehr Austria Salzburg
CustomerID CompanyName Country City ERNSH Ernst Handel Austria Graz
ALFKI Alfreds Futter… Germany Berlin .. … … …
ANATR Ana Trujillo… Mexico México D.F.
ANTON Antonio M ore… Mexico México D.F. Filtering Query Result
AROUT Around the… UK London  To Filter or retrieve specific results, use WHERE clause
.. .. .. ..  The Where clause is used to limit the rows that are
included in the result set.
Use TOP clause  The WHERE clause applies criteria that each row must
 TOP clause limit the number of rows returned meet
 Syntax  You can use = (equal), >, >=, <, =<, <> (not equal),
SELECT TOP n column-list FROM table-name between, in, like, is, exists operators
→ returns top n rows of the result-set

2
 You can use AND, OR and NOT Boolean operators to
combine multiple conditions
SELECT CustomerID, CompanyName, Country, City
FROM Customers
WHERE country = ‘Finland’
GO
 Output
CustomerID CompanyName Country City
WARTH Wartian Herkku Finland Oulu
WILMK Wilman Kala Finland Helsinki
 You can use Boolean operators (AND, OR, NOT) to
combine conditions.
SELECT CustomerID, CompanyName, Country, City
FROM Customers
WHERE country = 'Finland' OR country = 'Austria’
GO
 Output
CustomerID CompanyName Country City
ERNSH Ernst Handel Austria Graz
PICCO Piccolo und mehr Austria Salzburg
WARTH Wartian Herkku Finland Oulu
WILMK Wilman Kala Finland Helsinki
 The above query can be written as
SELECT CustomerID, CompanyName, Country, City
FROM Customers
WHERE country IN ('Finland' , 'Austria’)
GO
→ IN requires a set of values and records, that have field
value equal to anyone in the set, are included
Filtering using LIKE operator
 LIKE determines whether a specific character string
matches a specified pattern.
 A pattern can include regular characters and wildcard
characters.
 Using wildcard characters makes the LIKE operator more
flexible than using the = and != string comparison
operators.
 SQL wildcards can substitute for one or more characters
when searching for data in a database.
 SQL wildcards must be used with the SQL LIKE operator.
 With SQL, the following wildcards can be used:
Wildcard Description Example
character
% Any string of WHERE title LIKE '%computer%'
zero or more finds all book titles with the
characters. word 'computer' anywhere in
the book title.
_ (underscore) Any single WHERE au_fname LIKE '_ean'
character. finds all four-letter first names
that end with ean (Dean,
Sean, and so on).
[] Any single WHERE au_lname LIKE '[C-
character P]arsen' finds author last
within the names ending with arsen and
specified starting with any single
range ([a-f]) or character between C and P,
set ([abcdef]). for example Carsen, Larsen,
Karsen, and so on. In range
searches, the characters
included in the range may
vary depending on the sorting
rules of the collation.
[^] Any single WHERE au_lname LIKE
character not 'de[^l]%' all author last names
within the starting with de and where
specified the following letter is not l.
range ([^a-f])
or set
([^abcdef]).

Try this

You might also like