SQL-Lesson #2

SELECT

-- Choose one column
select email from users;

-- Choose more columns
select email, firstName, lastName from users;

-- Choose all columns
select * from users;

-- Use 'DISTINCT' to choose all unique data
select distinct role from users;

AS Clause

-- SQL aliases are used to give a table, or a column in a table, a temporary name.
select notified_delete_account_at as n from users;

-- You can combine alias with other columns
select notified_delete_account_at as n, firstName from users;

WHERE

 
-- Use 'where' condition as a search condition
select firstName, lastName, email from users where firstName = 'Sterling';

-- Use 'or' condition to choose one of the statements
select firstName, lastName, email from users where firstName = 'Sterling' or firstName = 'pm';

-- Use 'and' condition to choose both statements
select firstName, lastName, email from users where firstName = 'Sterling' and lastName = 'Lind';

-- Finds any values that end with "patso.com"
select email from users where email like '%patso.com';

-- Finds any values that have "@patso" in the third position
select email from users where email like '__@patso%';

WHERE 2.0

 
-- Use 'NULL' operator to choose all null or not null values
select email, deleted_at from users where deleted_at is null;

-- Another example with not null
select email, deleted_at from users where deleted_at is not null;

-- The 'BETWEEN' operator selects values within a given range. The values can be numbers, text, or dates.
select * from users where id between 99999998 and 100000010;

-- The 'ORDER BY' keyword is used to sort the result-set in ascending or descending order.
select * from users order by created_at asc;

select * from users order by created_at desc;

-- The 'LIMIT' keyword is used to limit the number of rows returned in a query result
select * from users order by created_at desc limit 4;

Task

1. Select from the table transactions_accounts columns: id, name, created_at, deleted_at.

- Use alias for name column should be transaction_category;

- id should be between 117 and 3217;

- created_at should be not null;

- name should have General inside;

- sort by created_at column in ascending order;

- limit the query result to one result

🤓
 

The result should be like this:

Resource

SQL - Lesson #2

By TenantCloud

SQL - Lesson #2

  • 233