Intro

This is T-SQL (Microsoft implementation of SQL);

SELECTION

The most important thing you need to know about SELECT is it doesn't affect the query.

  • TOP

-- Returns 5 rows 
select TOP(5) name from people

-- Returns 5% of rows 
select TOP(5) PERCENT name from people
  • DISTINCT if you use DISTINCT with two or more columns, it will return each unique combination of values

  • * Return everything, but not suitable for production or large tables, it's recommended to specifiy columns names

select * from people;
  • Ordering Queries return setws, or subsets, sets have no inherent order. so running

select * from people

will return different order for each time we run it.

we can order by any column, even if it does not appear on the select part of the query.

-- Select the top 20 rows from description, nerc_region and event_date
SELECT 
  TOP (20) description,
  nerc_region,
  event_date
FROM 
  grid 
  -- Order by nerc_region, affected_customers & event_date
  -- Event_date should be in descending order
ORDER BY
  nerc_region,
  affected_customers,
   event_date desc;

-- Select all artists beginning with B who released tracks in 1986, but also retrieve any records where the release_year is greater than 1990

GROUPS, SORTING, COUNTING, STRINGS

  • GROUPS

when using aggregate functions, every column you use in the select needs to be in an aggregate function, or will be used as grouping column.

e.g the following query will return an error

so be careful when using aggregate functions.

  • STRINGS working with strings in T-SQL is common, so take your time to master it, because you will need it alot.

  • LEN(column) returns the length of a string

name
name_length

yousef

6

bassant

7

It's useful to know the total length of a string, as a starting point for use in other string calculations. T-SQL provide many methods for manipulating strings in very easy manner

  • LEFT(column, number_of_char) extract number of characters from beginning of a string

description
first_20_left

Severe Weather Thunderstorms

Severe Weather Thun

Severe Weather Thunderstorms

Severe Weather Thun

Severe Weather Thunderstorms

Severe Weather Thun

Fuel Supply Emergency Coal

Fuel Supply Emergenc

Physical Attack Vandalism

Physical Attack Van

Severe Weather Thunderstorms

Severe Weather Thun

  • RIGHT (extract number of characters from beginning of a string)

  • CHARINDEX(char or pattern, column) help us find specific character index within a string or to be more precised, it returns the first index occurance of the given char or pattern

char_location
url

34

datacamp.com/courses/introduction_

34

datacamp.com/courses/intermediate_

29

datacamp.com/courses/writing_

29

datacamp.com/courses/joining_

27

datacamp.com/courses/intro_

  • SUBSTRING(column, start, end) sometimes we need to extract from middle of string as opposed to right or left. substring method comes to rescue!

target_section
url

datacamp.com

https//www.datacamp.com/courses

  • REPLACE(column, charToBeReplaced, charToReplaceWith)

replace_with_hyphen

datacamp.com/courses/introduction-

datacamp.com/courses/intermediate-

datacamp.com/courses/writing-

datacamp.com/courses/joining-

datacamp.com/courses/intro-

some exercises

-- select Weather from a description column Before writing any sql query, wether it seems complex of simple. Divide the query into smaller steps you can take first. then conquer the rest one after the other.

for this query, we need to get slice the string, from the first character to the length of word Weather

  1. find the first index of Weather

  2. calculate the length of the word

  3. use substring to extract the word

  • Gouping When we write a WHERE Clause, the filtering takes place on the row level - that is, within the data.

Where is used to filter rows before any grouping occurs

but what if we need to sum values based on groups?

GROUP BY and Having come to rescue.

simple exercise

notice the order of statements ORDER BY always comes last Having after GROUP BY

JOINS

I have dedicated notes for joins, you can refer to better than this.

DDL and DML statements

  • DDL

  • CREATE You have to consider some points when creating a table

  • table and column names

  • Type of data each column will store

  • Size or amount of data stored in the column

  • INSERT used to add or append data.

INSERT SELECT statement Inserting from table into another table.

  • Don't use SELECT *

  • Be specific in case table structure changes

  • UPDATE

  • DELETE Deleting happend immediately, so there is no confirmation message when deleting, be careful

-TRUNCATE Clears the entire table at once

there are some differences between truncate and delete, I will come to it later.

Declaring Variables And temporary tables

to avoid repetition, create a variable

DECLARE

  • Temporary Table: Sometimes you might want to 'save' the results of a query so you can do some more work with the data A table we can create for 'our session' only, to do some querying on it further, we can manually drop it.

some exerices

-- get the longest track from every album and add that to a temporary table

Last updated