sql Flashcards

1
Q

upercase = lowercase # are the same

A

SELECT first_name FROM patients where first_name like ‘C%’
SELECT first_name FROM patients where first_name like ‘c%’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

like

A

SELECT first_name FROM patients where first_name like ‘C%’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

between:
is >= <=
not ><

A

SELECT first_name, last_name,weight
FROM patients
WHERE weight BETWEEN 100 AND 120;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

operation card

A

– Uso del operador AND
SELECT *
FROM your_table
WHERE condition1 AND condition2;

– Uso del operador OR
SELECT *
FROM your_table
WHERE condition1 OR condition2;

– Uso del operador NOT
SELECT *
FROM your_table
WHERE NOT condition;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

con NULL nunca va =

A

update patients set allergies=’NKA’ where allergies is null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

concatenar || && concat(, , ,)

A

SELECT first_name || ‘ ‘ || last_name FROM patients
SELECT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

join on

A

SELECT table1.col1, table2.col3 where table
join on table2 table1.col2 = table2.col4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

view all the tables => sql server

A

select * from sys.tables
select name from sys.tables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

delete all elements of a table

A

delete Personas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

drop table

A

drop table Personas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

create a table with date

A

CREATE TABLE Personas ( last_name NVARCHAR(50),
birthday DATE
);

also works => string
CREATE TABLE Personas ( last_name NVARCHAR(50),
birthday NVARCHAR(50),
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

do questions

A

SELECT * FROM patients where Year(Birthday)=2010

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

count

A

SELECT count(*) FROM patients

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

max

A

example more complet

SELECT max(height) FROM patients

SELECT first_name,last_name,height FROM patients
where height= (SELECT max(height) FROM patients)

SELECT first_name,last_name,MAX(height) as height FROM patients

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

in

A

SELECT * FROM patients
where patient_id in (1,45,534,879,1000)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

top

A

select top(10) * from Personas // in SQL SERVER
select * from Personas LIMIT 10 // in SQL normal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

compare columns

A

SELECT * FROM admissions where admission_date = discharge_date

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

distinct

A

SELECT distinct(city) FROM patients /* all patients*/

SELECT distinct(city) FROM patients where province_id=’NS’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

order by

A

SELECT * FROM patients order by height

SELECT * FROM patients order by height DESC

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

group by

los valores de ONE no se van a repetir => group by ONE
los valores de TWO no se van a repetir => group by ONE, TWO

A

SELECT year(birth_date) FROM patients group by year(birth_date)

alse work (important) in sql server dont work
SELECT * FROM admissions group by admission_date

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

having
only use when you use => GROUP BY
it is like a WHERE

A

example 1

select first_name from patients group by first_name
having count(first_name)=1

select first_name from patients group by first_name having last_name = ‘a%’

21
Q

least 6 characters long

A

SELECT
patient_id,
first_name
FROM patients
WHERE first_name LIKE ‘s____%s’;

22
Q

len

A

SELECT
patient_id,
first_name
FROM patients
WHERE first_name LIKE ‘s%s’ and len(first_name)>=6;

23
Q

Order with 2 values

Order the list by the length of each name and then by alphabetically.

A

SELECT
first_name
FROM patients order by len(first_name), order by first_name

24
Q

create a table of horizontal

A

simple

select
(SELECT count(gender) FROM patients where gender=’M’),
(SELECT count(gender) FROM patients where gender=’F’)

select
(SELECT count(gender) FROM patients where gender=’M’) as one,
(SELECT count(gender) FROM patients where gender=’F’) as two

select gender,count(*) from patients group by gender

25
Q

union
a=[1,2,3] b=[2,3,4] a UNION b =[1,2,3,4]

union all
a=[1,2,3] b=[2,3,4] a UNION b =[1,2,3,4]

A

SELECT first_name, last_name, ‘Doctor’ FROM doctors
union ALL /UNION/
SELECT first_name, last_name, ‘Patient’ FROM patients

26
Q

UPPER & LOWER

A

SELECT upper(last_name) from patients
SELECT lower(last_name) from patients

27
Q

HAVING is like WHERE => in group by

A
28
Q

sum

A

SELECT province_id,sum(height) as two FROM patients group by province_id

29
Q

IMPORTANT => works also in SQL SERVE

A

example 3

select emp_no,COUNT(*) from works_on group by emp_no

30
Q

subtract // RESTAR

A

example 1

SELECT MAX(weight) - MIN(weight) FROM patients

SELECT 200 - 100 FROM patients
=> 100 // equal columns of table

31
Q
A
32
Q

where & having

where => go first
having => second

A
33
Q

ALLOW

HAVING admission_date = MAX(admission_date); //well

WHERE admission_date = MAX(admission_date); //wrong

A
34
Q

you can use MAX whit date

A

select max(admission_date) from admissions // 2019

select min(admission_date) from admissions // 2018

35
Q

work => % // equal JS

A

where (patient_id % 2 =1 // odd number :)

36
Q

use at the same time => JOIN & GROUP BY

A

SELECT first_name, last_name, COUNT(*) as admissions_count
FROM admissions
JOIN doctors ON doctors.doctor_id = admissions.attending_doctor_id
GROUP BY first_name, last_name
ORDER BY admissions_count DESC;

37
Q

producto cartesiano

A

select * from TABLE1,TABLE2

38
Q

you can join => min, max, name_expample * from aaa group by

For each doctor, display their id, full name, and the first and last admission date they attended.

A

select doctor_id , first_name,max(admission_date),min(admission_date) from doctors
join admissions on doctors.doctor_id = admissions.atending.doctor_id group by first_name, doctro_id

39
Q

case

A

SELECT
nombre,
CASE
WHEN edad < 18 THEN ‘Menor de edad’
WHEN edad >= 18 AND edad < 65 THEN ‘Adulto’
ELSE ‘Jubilado’
END AS grupo_edad
FROM
personas;

40
Q

left join // right join

table:ONE
id nombre precio
1 Camisa 5.00
2 Pantalón
3 Zapatos

table:TWO
id Annoyed
1 talkative
2 reliable

A

ONE left join TWO

ONE.id nombre precio Annoyed
1 Camisa 25.00 talkative
2 Pantalón 40.00 reliable
3 Zapatos 50.00 NULL

TWO.id nombre precio Annoyed
1 Camisa 25.00 talkative
2 Pantalón 40.00 reliable

41
Q

floor

A

floor(1.5) = 1
floor(1.1) = 1
floor(1.99) = 1
floor(2) = 2

42
Q

POWER # pow :)

A

POWER(height/100.0,2)

43
Q

LIMIT

A

select one,two,three from patients limit 100

44
Q

divide // how to divide (divaid)

100/7

A

wrong

select 100/7 from patients limit 1
=> 14

well
select (100100.0)/(7100) from patients limit 1
=> 14.285

45
Q
A

wrong

well
select sum(gender=’M’) from patients
select sum(gender=’F’) from patients

select count(gender=’M’) from patients
select count(gender=’F’) from patients

46
Q
A
46
Q
A
46
Q
A
47
Q
A