La presentazione è in caricamento. Aspetta per favore

La presentazione è in caricamento. Aspetta per favore

SQL Esercitazione per il corso “Basi di Dati” Gabriel Kuper

Presentazioni simili


Presentazione sul tema: "SQL Esercitazione per il corso “Basi di Dati” Gabriel Kuper"— Transcript della presentazione:

1 SQL Esercitazione per il corso “Basi di Dati” Gabriel Kuper
Nataliya Rassadko

2 Outline Lab structure What is SQL Simple queries Exercises Selection
Projection Condition Exercises

3 Lab structure List of students (sorted in alphabetical order)
Every lab, students will be asked to perform different exercises according to the list Every exercise is evaluated from 0-3 0 demonstrates very shallow performance 3 demonstrates an excellent performance These points can be VERY helpful in obtaining exam grade Let’s say, 5% of top-performing students will receive + 5 points for exam. Should be thought yet Yor main goal is to obtain as many points as possible Try not to skip lectures and especially labs!

4 Lab structure: Easter egg
Home-work! -3 points if chosen students do not know answers for the home-work

5 What is SQL? Structured Query Language
Language of queries for RDBMS (Relational Database Systems). Language for IBM DB2 and SQL/DS Oracle PL-SQL MS SQL Server T-SQL 1986: standard ANSI (American National Standards Institute) 1987: standard ISO (International Standards Organization) We will use PostgreSQL

6 Structure of SQL Declarative language (i.e., based on properties of retrieved data rather than on operations) DDL (Data Definition Language) – create/drop/alter table/database DML (Data Manipulation Language) – select/insert/delete DCL (Data Control Language) – grant/revoke

7 5 minute session What is a tuple? What is a relation?
What is an attribute? What is a relational data model? What is a relation instance?

8 Running Example Movie (title, year, length, inColor, studioName, producerC#) StarsIn (movieTitle, movieYear, starName) MovieStar (name, address, gender, birthdate) MovieExec (name, address, cert#, netWorth) Studio (name, address, presC#) What is the primary key? What is the foreign key? What are the probable primary and foreign keys for tables?

9 SELECT Query selects a set of tuples (relation) satisfying some conditions In procedural languages, there are packages that may parse the returned relation Consists of the following main clauses SELECT <list of attributes> FROM <list of tables> WHERE <condition> Like σC in relational algebra

10 1 minute session What is projection?
What is a relation algebra formula for query SELECT L FROM R WHERE C? πL(σC(R))

11 SELECT Example SELECT * FROM Movie
WHERE studioName=‘Disney’ and year=1990

12 DISTINCT SELECT * FROM StarsIn SELECT DISTINCT MovieTitle FROM StarsIn

13 Projection Tuple is extracted partially (some attributes)
SELECT title, length FROM Movie WHERE studioName=‘Disney’ and year=1990

14 Projection: Alias Rename column
SELECT title AS name, length AS duration FROM Movie WHERE studioName=‘Disney’ and year=1990 name duration

15 Projection: Operation on column values
All values of column undergo equal modification SELECT title AS name, length * AS duration FROM Movie WHERE studioName=‘Disney’ and year=1990

16 Projection: Constant columns
Add column with constant SELECT title AS name, length * AS duration, ‘hrs.’ as inHours FROM Movie WHERE studioName=‘Disney’ and year=1990

17 Condition in WHERE Condition in WHERE should be evaluated to boolean
True – tuple is included in result False – tuple is not included in result Comparison operators: <, >, =, <=, >=, < > do not confuse with C/Java != Comparison to strings studioName=‘Disney’ Arithmetic operators: +, -, *, / WHERE (year -1930) * (year -1930) < 100 Will return tuples with year in [ ] String and set operators Like, in, between, exists Boolean operators AND, OR, NOT

18 ESERCIZIO: operatori logici
Data la seguente tabella: movie(title, year, length, inColor, studioName, producerC#) visualizzare l’elenco dei film (titolo) a colori prodotti prima dell’anno 1965. SELECT ‘title’ as titolo FROM ‘movie’ WHERE ‘inColor’=“true” AND ‘year’< “1965”

19 ESERCIZIO: operatori logici
Data la seguente tabella: movie(title, year, length, inColor, studioName, producerC#) visualizzare l’elenco dei film (titolo) prodotti negli anni 1979 e 1971. SELECT ‘title’ as titolo FROM ‘movie’ WHERE ‘year’=“1971” OR ‘year’< “1979”

20 ESERCIZIO: operatori logici
Data la seguente tabella: movie(title, year, length, inColor, studioName, producerC#) visualizzare l’elenco dei film (titolo) in bianco e nero (non a colori). SELECT ‘title’ as titolo FROM ‘movie’ WHERE NOT ‘inColor’

21 ESERCIZIO: operatori logici
Data la seguente tabella: movie(title, year, length, inColor, studioName, producerC#) Visualizzare l’elenco dei film (titolo) prodotti dallo studio 'Universal Pictures [us]’ dopo l’anno 1970 o dalla durata minore di 90 minuti. SELECT ‘title’ as titolo FROM ‘movie’ WHERE (‘studioName’= “Universal Pictures [us]” AND ‘year’ = “1979”) OR ‘lenght’<“90”

22 ESERCIZIO Movie (title, year, length, inColor, studioName, producerC#)
Date la seguenti tabelle: Movie (title, year, length, inColor, studioName, producerC#) MovieExec (name, address, cert#, netWorth) Visualizzare i nomi dei produttori di film con durata minore di 200 minuti e che siano stati prodotti prima dell’anno 1980 o che siano a colori SELECT ‘name’ FROM ‘movie’, ‘MovieExc’ WHERE (‘name’=‘ producerC’ And (‘length’< “200” AND ‘year’ < “1980”)) OR (name’=‘ producerC’ ‘inColor’==“true”)

23 ESERCIZIO Movie(title, year, length, inColor, studioName, producerC#)
Data la seguente tabella: Movie(title, year, length, inColor, studioName, producerC#) Visualizzare i film prodotti in bianco e nero negli anni che vanno dal 1970 al 1990. SELECT ‘title’ FROM ‘movie’ WHERE ‘year’>= 1970 and ‘year’<= 1990 and inColor=“true”

24 ESERCIZIO Movie(title, year, length, inColor, studioName, producerC#)
Data la seguente tabella: Movie(title, year, length, inColor, studioName, producerC#) Visualizzare i titoli dei film (come Titolo) prodotti dopo il 1990. SELECT ‘title’ AS ‘Titolo’ FROM ‘movie’ WHERE ‘year’> 1990

25 ESERCIZIO Movie(title, year, length, inColor, studioName, producerC#)
Data la seguente tabella: Movie(title, year, length, inColor, studioName, producerC#) Visualizzare la lunghezza in ore di tutti i film prodotti in bianco e nero. SELECT ‘lenght’ * AS ‘length’, FROM ‘movie’ WHERE NOT ‘inColor’

26 ESERCIZIO Data la seguente tabella:
Movie(title, year, length, inColor, studioName, producerC#) Visualizzare in una tabella con intestazione (Anno,titolo, Studio),tutti i film in bianco e nero, prodotti dopo l’anno 1990 ma prima del 1995. SELECT yeas as Anno, title as Titolo, studioName as Studio FROM ‘movie’ WHERE not ‘inColor’ AND year>’1990’ AND year<‘1995’

27 ESERCIZIO Data la seguente tabella:
Movie(title, year, length, inColor, studioName, producerC#) Visualizzare tutti gli attributi dei film della serie shrek SELECT * FROM ‘movie’ WHERE title=’shrek’ or title=‘shrek 2’ or title=‘shrek 3’

28 ESERCIZIO Data la seguente tabella
Scrivere la query che restituisce i nomi presenti Scrivere la query che restituisce i cognomi presenti Scrivere la query che restituisce le età presenti Scrivere la query che restituisce nome, cognome e residenza di tutti i clienti Scrivere la query che restituisce tutti i dati dei clienti Clienti Cognome Nome Città Salario Età

29 ESERCIZI Data la seguente tabella Clienti Cognome Nome Città Salario
Scrivere la query che restituisce nome e cognome di chi guadagna più di 2000; Scrivere la query che restituisce cognome e nome dei clienti che abitano a Trento; Scrivere la query che restituisce cognome e nome dei clienti che abitano a Trento e guadagnano più di 2500; Scrivere la query che restituisce cognome, nome e salario dei clienti che hanno età compresa fra 20 e 40 anni (estremi compresi); Scrivere la query che restituisce cognome, nome e salario dei clienti che risiedono a Milano e hanno meno di 20 anni o più di 30. Clienti Cognome Nome Città Salario Età 29

30 Questions?


Scaricare ppt "SQL Esercitazione per il corso “Basi di Dati” Gabriel Kuper"

Presentazioni simili


Annunci Google