0% found this document useful (0 votes)
50 views5 pages

8 Simple SQL Queries

The document provides examples of basic SQL queries including selecting all or some columns from a table, limiting results, ordering results, removing duplicates, using comparison operators, logical operators (AND, OR, NOT), IN, BETWEEN, checking for NULL values, and wildcard searching. Some key SQL clauses and functions covered are SELECT, FROM, WHERE, ORDER BY, LIMIT, DISTINCT, LIKE, IN, BETWEEN, IS NULL.

Uploaded by

2023390023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

8 Simple SQL Queries

The document provides examples of basic SQL queries including selecting all or some columns from a table, limiting results, ordering results, removing duplicates, using comparison operators, logical operators (AND, OR, NOT), IN, BETWEEN, checking for NULL values, and wildcard searching. Some key SQL clauses and functions covered are SELECT, FROM, WHERE, ORDER BY, LIMIT, DISTINCT, LIKE, IN, BETWEEN, IS NULL.

Uploaded by

2023390023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SIMPLE SQL QUERIES

Basic SELECT, All Columns

SELECT * FROM <table_name>

e.g:

SELECT * FROM employee

Basic SELECT, Selected Columns

SELECT <column_1>, <column_2>, …, <column_n> FROM <table_name>

e.g:

SELECT emp_num, emp_fname, emp_lname FROM employee

Retrieving All Columns, But Limited Rows

SELECT * FROM <table_name>


LIMIT <number>

e.g:

SELECT * FROM employee


LIMIT 3

Select with Ordered Output

ASC – ascending order


DESC – descending order
*Instead of using column name(s) in ORDER BY clause, you can use number to represent column name
specified in SELECT clause as well

SELECT * FROM <table_name>


ORDER BY <column_1> ASC | DESC, <column_2> ASC | DESC

e.g:

SELECT * FROM employee


ORDER BY emp_area_code ASC, emp_fname DESC
Select with removing duplicate rows

SELECT DISTINCT <column_1>, <column_2>, …, column_n FROM <table_name>

e.g:

SELECT DISTINCT transaction_no FROM sales_line

SELECT line_no, ticket_no FROM sales_line -> SELECT DISTINCT line_no, ticket_no FROM sales_line

Select with Comparison Operators

SELECT * FROM ticket


WHERE column_name > comparison_value

e.g:

SELECT * FROM ticket


WHERE ticket_price > 15.00

SELECT * FROM employee


WHERE emp_fname = 'Anne'
Select with Condition(s) – AND, OR, NOT

1. AND only

SELECT <column_1>, <column_2>, ..., <column_3>


FROM <table_name>
WHERE <condition_1> AND <condition_2> AND <condition_3>

2. OR only

SELECT <column_1>, <column_2>, ..., <column_3>


FROM <table_name>
WHERE <condition_1> OR <condition_2> OR <condition_3>

3. NOT only

SELECT <column_1>, <column_2>, ..., <column_3>


FROM <table_name>
WHERE NOT <condition_1>

4. Mixed AND and OR

SELECT <column_1>, <column_2>, ..., <column_3>


FROM <table_name>
WHERE <condition_1> AND <condition_2> OR <condition_3>

e.g:

1. AND only

SELECT * FROM themepark


WHERE park_country = 'UK' AND park_city = 'WINDSOR'

2. OR only

SELECT * FROM themepark


WHERE park_country = 'UK' OR park_country = 'SW'

3. NOT only

SELECT * FROM themepark


WHERE park_country = 'UK' AND NOT park_city = 'STOKE'

Alternative for NOT, using comparison operator

SELECT * FROM themepark


WHERE park_country = 'UK' AND park_city != 'STOKE'
4. Mixed AND and OR

SELECT * FROM attraction


WHERE park_code = 'FR1001'
AND attract_name = 'Ant-Trap'
OR attract_age < 11

Select using IN, BETWEEN

SELECT * FROM themepark


WHERE park_country IN ('UK', 'SW')

SELECT * FROM attraction


WHERE attract_age BETWEEN 5 AND 11

Select with Null comparison

* Insert this record first into your database

INSERT INTO employee VALUES (107, 'Mr', 'Supian', 'Ali', '1980-02-05', '2008-05-02', '', '', NULL)

SELECT * FROM <table_name> WHERE <column_1> IS NULL

SELECT * FROM <table_name> WHERE <column_1> IS NOT NULL

e.g:

SELECT * FROM employee WHERE park_code IS NULL

SELECT * FROM employee WHERE park_code IS NOT NULL

Wildcard Searching

Characters can be used - %, _

SELECT * FROM <table_name> WHERE column_1 LIKE 'A%'

SELECT * FROM <table_name> WHERE column_1 LIKE '_n%'

e.g:

SELECT * FROM employee WHERE emp_fname LIKE 'A%'

SELECT * FROM employee WHERE emp_fname LIKE '__m%'

You might also like