Bem-vindo

Introdução ao SQL Server

John MacKintosh

Instructor

shutterstock_1255111450.jpg

shutterstock_418087330.jpg

Introdução ao SQL Server

SQL Server e Transact-SQL

  • SQL Server: sistema de banco de dados relacional da Microsoft

  • Transact-SQL (T-SQL): implementação da Microsoft com recursos extras

  • Neste curso: domine o básico de T-SQL

  • Aprenda a escrever consultas

Introdução ao SQL Server

shutterstock_274642010.jpg

Introdução ao SQL Server

Consultas 101

  • SQL Server: a loja com bancos e tabelas

  • Consultas: como pegamos itens de corredores diferentes e enchemos o carrinho

  • SELECT: termo-chave para buscar dados

Introdução ao SQL Server
SELECT description 
FROM grid;
+-------------------------------------+
| description                         |
|-------------------------------------|
| Severe Weather Thunderstorms        |
| Severe Weather Thunderstorms        |
| Severe Weather Thunderstorms        |
| Fuel Supply Emergency Coal          |
| Physical Attack Vandalism           |
| Physical Attack Vandalism           |
| Physical Attack Vandalism           |
| Severe Weather Thunderstorms        |
| Severe Weather Thunderstorms        |
| Suspected Physical Attack           |
| Physical Attack Vandalism           |
| ...                                 |
+-------------------------------------+
Introdução ao SQL Server

Selecionando mais de uma coluna

SELECT 
  artist_id, 
  artist_name 
FROM 
  artist;
+-----------+----------------------+
| artist_id | artist_name          |
|-----------+----------------------|
| 1         | AC/DC                |
| 2         | Accept               |
| 3         | Aerosmith            |
| 4         | Alanis Morissette    |
| 5         | Alice In Chains      |
| 6         | Antônio Carlos Jobim |
| 7         | Apocalyptica         |
| 8         | Audioslave           |
| 9         | BackBeat             |
| 10        | Billy Cobham         |
+-----------+----------------------+
Introdução ao SQL Server

Formatação de consultas

SELECT description, event_year, event_date
FROM grid;
SELECT 
  description, 
  event_year, 
  event_date
FROM 
  grid;
Introdução ao SQL Server

SELECT TOP ()

-- Retorna 5 linhas
SELECT TOP(5) artist
FROM artists;
-- Retorna os 5% do topo
SELECT TOP(5) PERCENT artist 
FROM artists;
+-----------------------+
| artist                |
|-----------------------|
| AC/DC                 |
| Accept                |
| Aerosmith             |
| Alanis Morissette     |
| Alice in Chains       |
+-----------------------+
Introdução ao SQL Server

SELECT DISTINCT

-- Retorna todas as linhas da tabela
SELECT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| RFC         |
| RFC         |
| MRO         |
| MRO         |
| ....        |
+-------------+
-- Retorna linhas únicas
SELECT DISTINCT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| NPCC        |
| NPCC RFC    |
| RFC         |
| ERCOT       |
| ...         | 
+-------------+
Introdução ao SQL Server

SELECT *

-- Retorna todas as linhas
SELECT * 
FROM grid;
  • NÃO recomendado para tabelas grandes
Introdução ao SQL Server

Apelidando colunas com AS

SELECT demand_loss_mw AS lost_demand 
FROM grid;
+-------------+
| lost_demand |
|-------------|
| 424         |
| 217         |
| 494         |
| 338         |
| 3900        |
| 3300        |
+-------------+
SELECT description AS cause_of_outage 
FROM grid;
+------------------------------+
| cause_of_outage              |
|------------------------------|
| Severe Weather Thunderstorms |
| Fuel Supply Emergency Coal   |
| Physical Attack Vandalism    |
| Suspected Physical Attack    |
| Electrical System Islanding  |
+------------------------------+
Introdução ao SQL Server

Vamos escrever um pouco de T-SQL!

Introdução ao SQL Server

Preparing Video For Download...