Evite erros comuns

Aplicando SQL a Problemas do Mundo Real

Dmitriy (Dima) Gorenshteyn

Lead Data Scientist, Memorial Sloan Kettering Cancer Center

Não use comentários de forma errada

/* Ao selecionar category e length
de films precisamos usar f; depois disso
comi um sanduíche, era um 
bom sanduíche ...*/
SELECT category, length
-- FROM actor as a
FROM film AS f
/* Inner join da tabela category 
com a tabela film */
INNER JOIN category AS c
ON f.film_id = c.film_id;

Não faça

  • Escrever um texto longo nos comentários.
Aplicando SQL a Problemas do Mundo Real

Não use comentários de forma errada





SELECT category, length
-- FROM actor as a
FROM film AS f
/* Inner join da tabela category 
com a tabela film */
INNER JOIN category AS c
ON f.film_id = c.film_id;

Não faça

  • Escrever um texto longo nos comentários.
  • Deixar comentários antigos no código final.
Aplicando SQL a Problemas do Mundo Real

Não use comentários de forma errada





SELECT category, length

FROM film AS f
/* Inner join da tabela category 
com a tabela film */
INNER JOIN category AS c
ON f.film_id = c.film_id;

Não faça

  • Escrever um texto longo nos comentários.
  • Deixar comentários antigos no código final.
  • Fazer comentários redundantes com o código.
Aplicando SQL a Problemas do Mundo Real

Não use comentários de forma errada

/* Ao selecionar category e length
de films precisamos usar f; depois disso
comi um sanduíche, era um 
bom sanduíche ...*/
SELECT category, length
-- FROM actor as a
FROM film AS f
/* Inner join da tabela category 
com a tabela film */
INNER JOIN category AS c
ON f.film_id = c.film_id;
SELECT category, length
FROM film AS f
INNER JOIN category AS c
ON f.film_id = c.film_id;
Aplicando SQL a Problemas do Mundo Real

Não use SELECT para tudo

SELECT *
FROM film AS f
INNER JOIN category AS c
ON f.film_id = c.film_id;
release_year language_id rental_duration rental_rate length  .......
2009            1            4              6.99        173  .......    
2006            1            7              6.99        185  .......    
2004            1            5              4.99        153  .......    
2007            1            7              2.99        69   .......
Aplicando SQL a Problemas do Mundo Real

Não use SQL para programar

DO $$
BEGIN
   FOR counter IN 1..5 LOOP
     IF (counter = 2) THEN
      RAISE NOTICE 'BINGO!';
     ELSE 
      RAISE NOTICE 'Not BINGO :-(';
     END IF;
   END LOOP;
END; $$
NOTICE:  1 Not BINGO :-(
NOTICE:  1 BINGO!
NOTICE:  3 Not BINGO :-(
NOTICE:  4 Not BINGO :-(
NOTICE:  5 Not BINGO :-(
Aplicando SQL a Problemas do Mundo Real

Vamos praticar!

Aplicando SQL a Problemas do Mundo Real

Preparing Video For Download...