SQL SELECTION Flashcards

1
Q

How to select all data from the table?

A

SELECT * FROM my_table_name;

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

How to select only rows which are equal by the condition?

A
# use =
SELECT * FROM my_table_name WHERE city = 'Berlin';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to select only rows which aren’t equal by the condition?

A
# use <>
SELECT * FROM my_table_name WHERE city <> 'Berlin';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to select only specific columns from the table? ( 2 ways)

A

define it explicitly instead of using *

SELECT city, country FROM customers;
# another way
SELECT customers.city, customers.country FROM customers;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to select values by many conditions?

A
# use OR / AND
SELECT * FROM Products WHERE price = 18 OR price = 19;

SELECT * FROM Products WHERE price = 18 AND name = ‘nik’;

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

Is it possible to return a result that is less or more than the given value?

A

Yes, use < or > or <=

SELECT * FROM Products WHERE price < 18

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

How to select only NULL/ not NULL values?

A

SELECT * FROM Products WHERE price IS NOT NULL;

SELECT * FROM Products WHERE price IS NULL;

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

How to select values by the last or first symbols?( 2 ways )

A

SELECT * FROM Customers WHERE city LIKE ‘B%n’
# => will return Berlin
SELECT * FROM Customers WHERE city LIKE ‘Be_n’
# => will return Bern
# ‘%’ - for many symbols, ‘_’ -> for only one symbol

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

How to select values from the specific range?

A
# use BETWEEN
SELECT * FROM Products WHERE price BETWEEN 18 AND 22;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to select values which are included in the array? Or not included?

A

use IN or NOT IN
SELECT * FROM Products WHERE name IN (‘nik’, ‘alex’)
SELECT * FROM Products WHERE name NOT IN (‘nic’,’alex’)

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

How to show a list of all databases?

A

SHOW DATABASES;

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

How to set the order of selection?

A

SELECT * FROM Products ORDER BY price;

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

How to set the order for several rows?

A

SELECT * FROM Products ORDER BY price, unit;

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

How to change order direction?

A

ASC - from low to up
# DESC - from up to low
SELECT * FROM Products ORDER BY price ASC;
SELECT * FROM Products ORDER BY price DESC;

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

How to get sum of the row?

A

SELECT SUM(price) FROM Products;

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

How to get sum fo the row by a group of values?

A

SELECT productName, SUM(price) FROM Products GROUP BY productName;

17
Q

How to get average value of the row?

A

SELECT AVG(price) FROM Products;

18
Q

How to get MIN/MAX value of the row?

A
SELECT MIN(price) FROM Products;
SELECT MAX(price) FROM Products;
19
Q

How to get a number of no empty row values from the table?

A

SELECT COUNT(price) FROM Products;

20
Q

How to return only unique value?

A

SELECT DISTINCT productName FROM Products;

21
Q

How to return limited number of values?

A

SELECT * FROM Products LIMIT 1;
# the following return 10 values starting from 0
SELECT * FROM Products LIMIT 0,10;

22
Q

How to temporary change the name of the selected column/table? ( 2 ways )

A

SELECT name AS another_name FROM profession;
# another without AS
SELECT name another_name FROM profession;

23
Q

How to select values with sub-request?

A

SELECT * FROM Products WHERE CategoryID IN (SELECT CategoryID FROM Categories);