The SELECT
clause in SQL
is used to specify the columns that you want to retrieve from a table
USE database_Name;
SELECT * FROM customers_table;
SELECT * FROM customers_table WHERE customer_id = 1;
SELECT * FROM customers_table ORDER BY first_name;
-- SELECT * FROM customers_table;
SELECT first_name, last_name, points FROM customers_table;
--- Order of math operators matter
SELECT first_name, last_name, points, points * 10 + 100 FROM customers_table;
SELECT
first_name,
last_name,
points,
points * 10 + 100 AS discount_factor
FROM customers_table;
--- Alias name should be sourounded by quotes in case there is a space between
SELECT DISTINCT state FROM customers_table;
The WHERE
clause in SQL
is used to filter the results of a query based on specific conditions. It allows you to specify criteria that must be met for a row to be included in the result set.
SELECT * FROM customers_table WHERE points > 3000;
-- Other operators: > >= < <= = != <>
SELECT * FROM customers_table WHERE state = "VA";
-- String "VA" is not case sensitive
SELECT * from customers_table WHERE state != "VA";
-- != and <> are equivalent
SELECT *
FROM customers_table
WHERE birth_date > "1990-01-01"; -- Date default format in sql: yyyy-mm-dd
AND
, OR
, and NOT
are logical operators used in SQL
to combine multiple conditions in the WHERE
clause.
Select * From customers_table WHERE birth_date > "1990-01-01" AND points > 1000;
-- Both conditions should be true
Select * FROM customers_table WHERE birth_date > "1990-01-01" OR points > 1000;
-- At least one of the conditions should be true
Selecting all customers that are born either after 1990 or they have more than 1000 points and live in virginia:
SELECT * from customers_table WHERE birth_date > "1990-01-01" OR points > 1000 AND state = "VA";
-- AND operator always evaluated before OR
Selecting all customers that are not born after january first 1990 or not have points more than 1000:
Select * FROM customers_table WHERE NOT (birth_date > "1990-01-01" OR points > 1000);
-- Equivalent to below:
Select * FROM customers_table WHERE birth_date <= "1990-01-01" AND points <= 1000;
The IN
operator in SQL
is used to check if a value exists within a list of values. It's a convenient way to simplify queries that involve multiple OR
conditions.
SELECT * FROM customers_table WHERE state = "VA" OR state = "GA" OR state = "FL";
-- Equivalent to below using IN operator:
Select * FROM customers_table WHERE state IN ("VA", "GA", "FL");
SELECT * FROM customers_table WHERE state NOT IN ("VA", "GA", "FL");
The BETWEEN
operator in SQL
is used to check if a value falls within a specified range. It's a convenient way to simplify queries that involve multiple comparison operators.
SELECT * FROM customers_table where points BETWEEN 1000 AND 3000;
-- Range is inclusive ---> 1000 <= points <= 3000
The LIKE
operator in SQL
is used for pattern matching. It allows you to search for rows that contain specific patterns within a column.
SELECT * FROM customers_table WHERE last_name LIKE "b%";
-- % means any number of characters - case insensitive
SELECT * FROM customers_table WHERE last_name LIKE "brush%";
SELECT * FROM cusomers_table WHERE last_name LIKE "%b%";
SELECT * FROM customers_table WHERE last_name LIKE "%y";
SELECT * FROM customers_table WHERE last_name LIKE "_____y";
-- _ means a single character
Selecting only customers whose length of their last name is 6 characters and starts with b
and ends with y
;
SELECT * FROM customers_table WHERE last_name LIKE "b____y";
The REGEXP
operator is a powerful tool for pattern matching in SQL
, providing more flexibility and advanced features compared to the LIKE
operator. While its availability and syntax may vary slightly across different database systems, the core concept remains consistent.
SELECT * FROM custoemrs_table WHERE last_name LIKE "%field%"
-- Equivalent to below:
SELECT * FROM customers_table WHERE last_name REGEXP "field";
SELECT * From customers_table WHERE last_name REGEXP "^field";
SELECT * FROM customers_table WHERE last_name REGEXP "field$";
SELECT * FROM customers_table WHERE last_name REGEXP "field|mac";
SELECT * FROM customers_table WHERE last_name REGEXP "field|mac|rose";
Selecting all customers whose last name ends with word field
or have words mac
or rose
in their last name:
SELECT * FROM customers_table WHERE last_name REGEXP "field$|mac|rose";
SELECT * FROM customers_table WHERE last_name REGEXP "[gim]e";
Selecting all customers who have range of characters from a
to h
before character e
in their last name:
SELECT * FROM customers_table WHERE last_name REGEXP "[a-h]e";
The IS NULL
operator in SQL is used to check if a value is null. A null value represents the absence of data.
SELECT * FROM customers_table WHERE phone IS NULL;
SELECT * FROM customers_table WHERE phone IS NOT NULL;
The ORDER BY
clause in SQL is used to sort the results of a query based on one or more columns. It allows you to arrange the rows in ascending
or descending
order.
SELECT * FROM customers_table ORDER BY first_name;
SELECT * FROM customers_table ORDER BY first_name DESC;
Selecting all customers and sort them based on their state
in ascending order and for each state
sort them based on their first name
in ascending order:
SELECT * FROM customers_table ORDER BY state, first_name;
Selecting all customers and sort them based on their state
in descending order and for each state
sort them based on their first name
in descending order:
SELECT * FROM customers_table ORDER BY state DESC , first_name DESC;
SELECT first_name, last_name FROM customers_table ORDER BY birth_date;
SELECT first_name, last_name, 10 AS points FROM customers_table ORDER BY points, first_name;
The LIMIT
clause in SQL
is used to specify the maximum number of rows to return from a query. It's particularly useful when dealing with large datasets
or when you only need a subset of the results.
SELECT * FROM customers_table LIMIT 3;
Implementing the pagination in sql so that skip first 6 records and get other 3 records after skip section:
SELECT * FROM customers_table LIMIT 6, 3;