0% found this document useful (0 votes)
0 views

SQL+Commands

This document provides an overview of SQL statements, including commands for creating, altering, and deleting tables, as well as querying data using various functions and conditions. It explains the syntax for common SQL operations such as SELECT, INSERT, UPDATE, and DELETE, along with the use of JOINs and filtering techniques. Additionally, it emphasizes the importance of careful execution of commands due to the irreversible nature of certain actions.

Uploaded by

4052748
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

SQL+Commands

This document provides an overview of SQL statements, including commands for creating, altering, and deleting tables, as well as querying data using various functions and conditions. It explains the syntax for common SQL operations such as SELECT, INSERT, UPDATE, and DELETE, along with the use of JOINs and filtering techniques. Additionally, it emphasizes the importance of careful execution of commands due to the irreversible nature of certain actions.

Uploaded by

4052748
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

SQL STATEMENTS

- Structured Query Language: SQL can be pronounced ‘Es-Que-El’ or ‘Sequel’ – both are correct and used
frequently
- SQL commands and table names are case insensitive – ‘SELECT * FROM TableName’ is the same as ‘select *
from tablename’
- Text strings within a record are case sensitive – searching for ‘North America’ is not the same as ‘north
america’

- Screenshots are from DB Browser (https://sqlitebrowser.org/).


- To run commands in DB Browser press either F5 or the Play button

ALTER

- Change the name of a table


- In DB Browser, need to close and reopen the database to see the table changes
- You cannot remove a column using ALTER. Instead, you would:
o 1) Create a new temporary table
o 2) Use INSERT to move the desired data from the original table to the new table
o 3) DROP the original table
o 4) Use ALTER to rename the temporary table

AND, OR, NOT

- Combine multiple conditions for filtering the records in your table

1
AS

- Use an alias for column names


- Useful for:
o Joining tables
o When you have long table names, to avoid having to retype them
o When performing calculations on columns

AVG

- Calculate the average number in a numeric column

2
BETWEEN

- Show results between two values


- Results are inclusive of the values used for the BETWEEN statement – in this example, a country with a
population of 1 million would also be returned

Comparisons

- Show results where a numerical field is greater or less than a given value

= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to

3
COUNT

- Count the number of entries matching your query

CREATE TABLE

- Create new table


- CREATE TABLE ‘tableName’ (
‘Column01’ [DataType] [Options],
‘Column02’ [DataType] [Options]
)
- Take note of the comma separating each column, after the options
- Data Types:
o INTEGER – a positive or negative number, up to 19 digits in length
o REAL – a positive or negative decimal number, up to 19 digits in length
o TEXT – text strings
o BLOB – Binary Large Object – files, video, images, etc
- Options
o NON NULL – For each new record, this field must contain a value, cannot be left blank
o PRIMARY KEY – a unique reference ID for each record in the table. Tables don’t have to have a
primary key, but it does help with organising and joining data
o AUTOINCREMENT – a numerical field which increases by 1 when a new record is added to the table.
This is useful in creating a primary key field
o UNIQUE – This indicates that all fields in this column must have unique entries

4
DELETE

- Clears the entry, or all entries & data from the table
- There is no way to reverse this. No recycle bin or Undo – backup your database before deleting it!

DROP TABLE

- Remove a table from the database


- There is no way to reverse this. No recycle bin or Undo – backup your database before deleting it!

GROUP BY

- Group records which have a common value (like continent), into summary rows
- Often used with calculation operators, like Sum, Count, etc

5
IN

- Use nested SQL queries, i.e. a query within a query


- Use the results of one query, to form the WHERE statement for a second query
- The first query (the query inside the IN statement) must return only one column of data

INSERT INTO

- Insert new records into the table


- If you are inserting a value for each column in the table, you don’t need to specify the columns
- If you are only entering values for some of the columns, you need to specify those columns
- Deduplicate the data in a table by:
o Creating a new table
o Use INSERT INTO new_table SELECT DISTINCT * FROM old_table
o Delete the original table, and rename the new table back to the original table name

6
JOINS: CROSS JOIN

- The least commonly used of the joins


- All entries in the first table are matched with all entries in the second table

table1 table2

A A

B B

JOINS: INNER JOIN

- The first table mentioned is treated as the LEFT table


- Results are shown only where they appear in both the left and right tables

table1 table2

7
JOINS: LEFT OUTER JOIN

- Show all results from the LEFT table (the first table mentioned in your query)
- Include any matching data from the second table (the RIGHT table)

table1 table2

8
LIKE

- Show results matching a text pattern


- Use % as a wildcard

LIMIT

- Limit the SELECT statement to only show a certain number of results

9
MAX, MIN

- Show the highest or lowest results from a numerical field

ORDER BY

- Sort the results of a query in alphabetical or numerical order


- By default, results are ordered in ascending order

10
SELECT

- Use * when you want to return all columns from the table
- To only return specific columns, specify the columns in the SELECT query

SELECT DISTINCT

- Remove duplicate values from the results of a SELECT query


- Doesn’t change the data in the database, only the results for the query

11
SUM

- Add up all values in a numerical column

UPDATE

- Update an existing record in the database with a new value


- Getting the WHERE statement correct is most important - If the WHERE statement is not included, all
records in the table will be changed
- Be sure of your statement before running the command – there is no undo with SQL

12
VIEWS

- A SELECT statement, stored under an easy to remember/use name. Useful for saving or storing very complex
select statements
- Read Only – Statements which modify data (DELETE, UPDATE, INSERT, …) cannot be part of a VIEW
- Create using ‘CREATE VIEW view_name AS’, then list the SQL statement
- As a virtual table, you can select all data from the VIEW, or only specific columns, data, etc. You can use
modifiers such as LIMIT, WHERE, LIKE, MAX, LIMIT, etc
- When dropping the view, take note that there is no way to reverse this. There is no recycle bin or Undo –
backup your database before deleting/changing it!

13

You might also like