670 likes | 995 Views
SQL Basics. What is a database?. Information file – better than plain text Structured Manipulable . . . What is a database?. QUERY. How does it work?. Database File Tables Attributes Rows Relationships between tables. How does it work?. DATABASE. Table 1. Table 2. Table N.
E N D
What is a database? • Information file – better than plain text • Structured • Manipulable • . . .
What is a database? QUERY
How does it work? • Database File • Tables • Attributes • Rows • Relationships between tables
How does it work? DATABASE Table 1 Table 2 Table N row 1 row 1 row 1 . . . row 2 row 2 row 2 row 3 row 3 row 3 … … … row N row M row X
Relationships Company DATABASE
SQL Start Programs MySQL MySQL Query Browser
Connecting Server Host: 10.100.34.37 Username: workshop Password: sql08
Selecting a Database show databases;
Selecting a Database use SQL_workshop;
Showing Tables show tables;
Showing Tables Snps Samples Genotypes
Showing Tables Describe snps;
Showing Tables Describe samples;
Showing Tables Describe genotypes;
Showing Tables SQL_workshop DATABASE
Querying a Table SELECT cols_name, ... FROM tbl_name ;
Our first query SELECT id_snp FROM snps ;
Our first query SELECT id_snp, chromosome FROM snps ;
Our first query SELECT * FROM snps ;
Our first query Exercices: • Get the id_sample and sex of all the samples • Get the genotype of all the genotypes • Get all the attributes of all the samples
Our first query SELECT * FROM samples ; SELECT id_sample, sex FROM samples ; SELECT genotype FROM genotypes ;
WHERE SELECT cols_name, ... FROM tbl_name WHERE condition ;
WHERE SELECT * FROM snps WHERE chromosome=“12” ;
WHERE SELECT * FROM snps WHERE position>2720000 ;
WHERE SELECT * FROM snps WHEREchromosome=“12” AND position>2720000 ;
WHERE Exercices: • Get all the female samples • Get all the CG genotypes • Get all the samples whose phenotype is “lung cancer” or “psoriasis”
WHERE SELECT * FROM samples WHERE phenotype=“lung cancer” OR phenotype=“psoriasis”; SELECT * FROM samples WHERE sex=“F”; SELECT * FROM genotypes WHERE genotype=“CG”;
Order SELECT cols_name, ... FROM tbl_name WHERE condition ORDER BY cols_name [ASC|DESC], ... ;
Order SELECT * FROM snps ORDER BY position ASC ;
Order SELECT * FROM snps ORDER BY position DESC ;
Order SELECT * FROM genotypes ORDER BY id_snp ASC, id_sample DESC ;
Relationships SQL_workshop DATABASE
Relationships SELECT cols_name, ... FROM tbl_name_1, ... WHERE joining_condition, ... ;
Relationships SELECT * FROM snps, genotypes ;
Relationships SELECT * FROM snps, genotypes WHERE snps.id_snp=genotypes.id_snp ;
Relationships SELECT * FROM snps s, genotypes g WHERE s.id_snp=g.id_snp ;
Relationships Exercices: • Get all the genotypes of all the MALE samples • Get all the information (snps, sample & genotype) of all the genotypes • Get all the information (snps, sample & genotype) of all the genotypes of the chromosome 12 of female samples
Relationships • Get all the genotypes of all the MALE samples SELECT * FROM samples s, genotypes g WHERE s.id_sample=g.id_sample AND s.sex=“M”; SELECT g.id_snp, s.id_sample, s.sex,s.age, s.phenotype, g.genotype FROM samples s, genotypes g WHERE s.id_sample=g.id_sample AND s.sex=“M”;
Relationships • Get all the information (snps, sample & genotype) of all the genotypes SELECT * FROM snps s, samples p, genotypes g WHERE s.id_snp=g.id_snp AND p.id_sample=g.id_sample; SELECT s.*, p.*, g.genotype FROM snps s, samples p, genotypes g WHERE s.id_snp=g.id_snp AND p.id_sample=g.id_sample;
Relationships • Get all the information (snps, sample & genotype) of all the genotypes of the chromosome 12 of female samples SELECT * FROM snps s, samples p, genotypes g WHERE s.id_snp=g.id_snp AND p.id_sample=g.id_sample AND s.chromosome=“12” AND p.sex=“F”; SELECT s.*, p.*, g.genotype FROM snps s, samples p, genotypes g WHERE s.id_snp=g.id_snp AND p.id_sample=g.id_sample AND s.chromosome=“12” AND p.sex=“F”;
Functions • Several SQL Functions • Not only OLD information • Dynamic information
Functions • COUNT • MIN / MAX • AVG • Numeric operators (+ - * / )
Functions • How many snps do I have? SELECT COUNT(*) FROM snps; SELECT COUNT(id_snp) FROM snps; SELECT COUNT(*) AS number_of FROM snps;
Functions • What is the min/max/average position of my snps? SELECT MIN(position) FROM snps; SELECT MAX(position) FROM snps; SELECT AVG(position) FROM snps;
Grouping things • How many snps do I have in each chromosome? SELECT chromosome, COUNT(*) FROM snps GROUP BY chromosome;