SQL select examples

This is part of tutorial learning SQL from beginners in 2018.
In this post there are several examples:

  • SELECT Statement
  • SELECT Where
  • SELECT AND OR NOT
  • SELECT Order BY
  • SELECT DISTINCT

Note: In order to skip schema name :

SELECT * FROM customers;
SELECT * FROM mydb.customers;

you need to write:

USE mydb;

SQL SELECT Statement

Simple select syntaxes

SELECT * FROM customers;
SELECT first_name, city FROM customers

SQL SELECT Where

Using conditions to filter results by one criteria

SELECT * FROM customers
WHERE country_region='USA'; 

SELECT * FROM customers
WHERE id=1; 

SQL SELECT AND OR NOT

Filtering select query results by several constraints

SELECT * FROM customers
WHERE country_region='USA' and city='Seattle';
	
SELECT * FROM customers
WHERE city='Las Vegas' or city='Seattle';

SELECT * FROM customers
WHERE not city='Seattle';

SQL SELECT ORDER BY

Ordering query results by several fields in ascending or descending order:

SELECT * FROM customers
ORDER BY city;

SELECT * FROM customers
ORDER BY city desc;

SQL SELECT DISTINCT

Return unique values from column by SQL query:

SELECT DISTINCT city FROM customers;

SELECT Count(*) FROM (SELECT DISTINCT city FROM customers) as cityCount;