My QA Projects

QA Projects I was involded.

View on GitHub

Excerpts from sqltutorial.org


SQL Syntax

SELECT 
    first_name
FROM
    employees
WHERE
    YEAR(hire_date) = 2000;

SQL commands

SELECT 
    first_name, last_name
FROM
    employees;

DELETE FROM employees 
WHERE
    hire_date < '1990-01-01';

Literals

# String literal consists of one or more alphanumeric characters surrounded by single quotes
'John'
'1990-01-01'
'50'

Keywords

Identifiers


-- statements are equivalent

Select  * From employees;

SELECT * FROM EMPLOYEES;

select * from employees;

SELECT * FROM employees;

Comments

SELECT 
    employee_id, salary
FROM
    employees
WHERE
    salary < 3000;-- employees with low salary
/* increase 5% for employees whose salary is less than 3,000 */
UPDATE employees 
SET 
    salary = salary * 1.05
WHERE
    salary < 3000;

Section 2: Querying Data

Introduction to SQL SELECT statement

Syntax

SELECT 
    select_list
FROM
    table_name;

1) SQL SELECT – selecting data from all columns example


SELECT * FROM table_name;

2) SQL SELECT – selecting data from specific columns

SELECT 
    employee_id, 
    first_name, 
    last_name, 
    hire_date
FROM
    employees;

3) SQL SELECT – performing a simple calculation

SELECT 
    first_name, 
    last_name, 
    salary, 
    salary * 1.05
FROM
    employees;
    -- salary * 1.05 adds 5% to the salary of every employee.
expression AS column_alias
SELECT 
    first_name, 
    last_name, 
    salary, 
    salary * 1.05 AS new_salary
FROM
    employees;
+-------------+-------------+----------+------------+
| first_name  | last_name   | salary   | new_salary |
+-------------+-------------+----------+------------+
| Steven      | King        | 24000.00 | 25200.0000 |
| Neena       | Kochhar     | 17000.00 | 17850.0000 |
| Lex         | De Haan     | 17000.00 | 17850.0000 |

Section 3: Sorting Data

SELECT 
    select_list
FROM
    table_name
ORDER BY 
    sort_expression [ASC | DESC];
SELECT 
    select_list
FROM
    table_name
ORDER BY 
    sort_expression_1 [ASC | DESC],
    sort_expression_2 [ASC | DESC];
SELECT
    employee_id,
    first_name,
    last_name,
    hire_date,
    salary
FROM
    employees
ORDER BY
    first_name;
+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         121 | Adam        | Fripp       | 1997-04-10 |  8200.00 |
|         115 | Alexander   | Khoo        | 1995-05-18 |  3100.00 |
|         103 | Alexander   | Hunold      | 1990-01-03 |  9000.00 |
|         193 | Britney     | Everett     | 1997-03-03 |  3900.00 |
|         104 | Bruce       | Ernst       | 1991-05-21 |  6000.00 |
|         179 | Charles     | Johnson     | 2000-01-04 |  6200.00 |
|         109 | Daniel      | Faviet      | 1994-08-16 |  9000.00 |
|         105 | David       | Austin      | 1997-06-25 |  4800.00 |
|         114 | Den         | Raphaely    | 1994-12-07 | 11000.00 |
|         107 | Diana       | Lorentz     | 1999-02-07 |  4200.00 |
...
SELECT
    employee_id,
    first_name,
    last_name,
    hire_date,
    salary
FROM
    employees
ORDER BY
    first_name DESC;
SELECT
    employee_id,
    first_name,
    last_name,
    hire_date,
    salary
FROM
    employees
ORDER BY
    salary DESC;
+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         100 | Steven      | King        | 1987-06-17 | 24000.00 |
|         101 | Neena       | Kochhar     | 1989-09-21 | 17000.00 |
|         102 | Lex         | De Haan     | 1993-01-13 | 17000.00 |
|         145 | John        | Russell     | 1996-10-01 | 14000.00 |
|         146 | Karen       | Partners    | 1997-01-05 | 13500.00 |
SELECT
    employee_id,
    first_name,
    last_name,
    hire_date,
    salary
FROM
    employees
ORDER BY
    hire_date;
SELECT
    employee_id,
    first_name,
    last_name,
    hire_date,
    salary
FROM
    employees
ORDER BY
    hire_date DESC;
+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         100 | Steven      | King        | 1987-06-17 | 24000.00 |
|         200 | Jennifer    | Whalen      | 1987-09-17 |  4400.00 |
|         101 | Neena       | Kochhar     | 1989-09-21 | 17000.00 |
+-------------+-------------+-------------+------------+----------+
| employee_id | first_name  | last_name   | hire_date  | salary   |
+-------------+-------------+-------------+------------+----------+
|         179 | Charles     | Johnson     | 2000-01-04 |  6200.00 |
|         113 | Luis        | Popp        | 1999-12-07 |  6900.00 |
|         119 | Karen       | Colmenares  | 1999-08-10 |  2500.00 |

Section 4: Filtering Data

SELECT DISTINCT
    column1, column2, ...
FROM
    table1;
SELECT 
    salary
FROM
    employees
ORDER BY salary DESC;

=> The result set has some duplicates.

SELECT 
    DISTINCT salary
FROM
    employees
ORDER BY salary DESC;

2) Using SQL DISTINCT operator on multiple columns example

SELECT
    job_id,
    salary
FROM
    employees
ORDER BY
    job_id,
    salary DESC;
OUTPUT
+--------+----------+
| job_id | salary   |
+--------+----------+
|      1 |  8300.00 |
|      2 | 12000.00 |
|      3 |  4400.00 |
|      4 | 24000.00 |
|      5 | 17000.00 |
|      5 | 17000.00 |
job_id  salary
4   24000
5   17000
5   17000
SELECT DISTINCT
    job_id,
    salary
FROM
    employees
ORDER BY
    job_id,
    salary DESC;

SQL DISTINCT and NULL

SELECT DISTINCT phone_number
FROM employees
ORDER BY phone_number;

=> Use DISTINCT operator in the SELECT clause to remove duplicate rows from the result set.

SELECT 
    column_list
FROM
    table1
ORDER BY column_list
LIMIT row_count OFFSET offset;
SELECT 
    employee_id, 
    first_name, 
    last_name
FROM
    employees
ORDER BY 
    first_name;

# adding now LIMIT, after it has been order makes it readable

SELECT 
    employee_id, 
    first_name, 
    last_name
FROM
    employees
ORDER BY 
    first_name
LIMIT 5;   
SELECT 
    employee_id, first_name, last_name
FROM
    employees
ORDER BY first_name
LIMIT 5 OFFSET 3;

# it will drop the first three and then display the next five
-- get the top five employees with the highest salaries
SELECT 
    employee_id, 
    first_name, 
    last_name, 
    salary
FROM
    employees
ORDER BY 
    salary DESC
LIMIT 5;
-- to get employees who have the 2nd highest salary
SELECT 
    employee_id, 
    first_name, 
    last_name, 
    salary
FROM
    employees
ORDER BY 
    salary DESC
LIMIT 1 OFFSET 1;

OFFSET offset_rows { ROW | ROWS }
FETCH { FIRST | NEXT } [ fetch_rows ] { ROW | ROWS } ONLY
SELECT 
    employee_id, 
    first_name, 
    last_name, 
    salary
FROM employees
ORDER BY 
    salary DESC
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY;
SELECT 
    employee_id, 
    first_name, 
    last_name, 
    salary
FROM employees
ORDER BY 
    salary DESC
OFFSET 5 ROWS
FETCH NEXT 5 ROWS ONLY;

SELECT 
    column1, column2, ...
FROM
    table_name
WHERE
    condition;
SELECT
    employee_id,
    first_name,
    last_name,
    salary
FROM
    employees
WHERE
    salary > 14000
ORDER BY
    salary DESC;
SELECT
    employee_id,
    first_name,
    last_name,
    department_id
FROM
    employees
WHERE
    department_id = 5
ORDER BY
    first_name;





















Section 5: Conditional Expressions



Section 6: Joining Multiple Tables


Section 7: Aggregate Functions


Section 8: Grouping Data


Section 9: SET Operators


Section 10. Subquery


Section 11: Modifying data


Section 12: Working with table structures


Section 13: Constraints



SQL in the cmd line

>> sqlite3
>> sqlite> .open HR.db
>> SELECT COUNT(*) FROM employees WHERE salary IS NOT NULL;

SELECT COUNT(*) FROM EVSUV_20242023 WHERE Year IS NOT NULL;
# OUTPUT
70

# To see a list of all available tables in your SQLite database 
# using the sqlite3 command-line interface
sqlite> .tables
EVSUV_20242023