Bienvenid@

Introducción a SQL Server

John MacKintosh

Instructor

Persona usando portátil con gráficos

Código SQL en pantalla

Introducción a SQL Server

SQL Server y Transact-SQL

  • SQL Server: sistema de base de datos relacional de Microsoft

  • Transact-SQL (T-SQL): implementación de SQL de Microsoft, con funciones extra

  • En este curso: domina lo básico de T-SQL

  • Aprende a escribir consultas

Introducción a SQL Server

Persona tecleando en portátil

Introducción a SQL Server

Consultas 101

  • SQL Server: el “supermercado” con bases de datos y tablas

  • Consultas: cómo “elegimos” productos de distintos pasillos y llenamos el carro

  • SELECT: término clave para recuperar datos

Introducción a 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           |
| ...                                 |
+-------------------------------------+
Introducción a SQL Server

Seleccionar más de una columna

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         |
+-----------+----------------------+
Introducción a SQL Server

Formato de consultas

SELECT description, event_year, event_date
FROM grid;
SELECT 
  description, 
  event_year, 
  event_date
FROM 
  grid;
Introducción a SQL Server

Select TOP ()

-- Devolver 5 filas
SELECT TOP(5) artist
FROM artists;
-- Devolver el 5% superior de filas
SELECT TOP(5) PERCENT artist 
FROM artists;
+-----------------------+
| artist                |
|-----------------------|
| AC/DC                 |
| Accept                |
| Aerosmith             |
| Alanis Morissette     |
| Alice in Chains       |
+-----------------------+
Introducción a SQL Server

Select DISTINCT

-- Devolver todas las filas de la tabla
SELECT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| RFC         |
| RFC         |
| MRO         |
| MRO         |
| ....        |
+-------------+
-- Devolver filas únicas
SELECT DISTINCT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| NPCC        |
| NPCC RFC    |
| RFC         |
| ERCOT       |
| ...         | 
+-------------+
Introducción a SQL Server

Select *

-- Devolver todas las filas
SELECT * 
FROM grid;
  • NO recomendable para tablas grandes
Introducción a SQL Server

Alias de columnas con 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  |
+------------------------------+
Introducción a SQL Server

¡Escribamos algo de T-SQL!

Introducción a SQL Server

Preparing Video For Download...