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

Basic SQL

This document provides an overview of basic SQL syntax for querying, manipulating, and defining data in relational database tables. It describes SQL statements for selecting data, filtering rows, sorting results, calculating values, inserting, updating, deleting rows, and joining tables. It also covers creating, altering, and dropping database objects like tables and views. Keywords, clauses, operators and functions commonly used in SQL are defined.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Basic SQL

This document provides an overview of basic SQL syntax for querying, manipulating, and defining data in relational database tables. It describes SQL statements for selecting data, filtering rows, sorting results, calculating values, inserting, updating, deleting rows, and joining tables. It also covers creating, altering, and dropping database objects like tables and views. Keywords, clauses, operators and functions commonly used in SQL are defined.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Mekelle University-CS Department ICT352

BASIC SQL SYNTAX

SELECT the Basic Query Statement


SELECT [DISTINCT] <column-name(s)>
FROM <table-name>;
Ordering Rows
SELECT [DISTINCT] <column-name(s)>
FROM <table-name>
ORDER BY <column-name(s)>[ASC/DESC]
Selecting Rows from a Table
SELECT <column-name(s)>
FROM <table-name>
WHERE <column-name><comparison operator><value>
Comparison Operators
= equal to
< less than
!= not equal to
<= less than or equal to
> greater than
> =greater than or equal to

Multiple Search Conditions


SELECT <column-name(s)>
FROM <table-name>
WHERE <condition> AND <condition>;
Alternate Search Conditions
SELECT <column-name(s)>
FROM <table-name>
WHERE <condition> OR <condition>;

Range Search
SELECT <column-name(s)>
FROM <table-name>
WHERE <column-name>
[NOT] BETWEEN <value-1 >AND <value-2>;
Matching Character Patterns
SELECT <column-name(s)>
FROM <table-name>
WHERE <column-name>
[NOT] LIKE <'character string'>;
Wildcards:
• % means any and all following characters are eligible (* in MS-
Access)
• _ means any one character (? In MS-Access)
Eg. [NOT] LIKE ‘cha%’

Searching for Nulls


SELECT <column-name(s)>
FROM <table-name>
WHERE <column-name>IS [NOT] NULL;

Set Comparison Operators


SELECT <column-name(s)>
FROM <table-name>
WHERE <column-name> [NOT] IN (<value-1 >, <value-2>....);

1 of 4 Prepared by: Tsegay S.


Mekelle University-CS Department ICT352

Calculations
SELECT <column-name(s)>,<arithmetic expression>
FROM <table-name>
[WHERE <condition>]
[ORDER BY <column-name(s)];

Arithmetic Operators
* Multiplication
/ Division
+ Addition
- Subtraction
Inserting Rows
INSERT INTO <table-name>
[(<column-name-l>, <column-name.2>,...)]
VALUES (<value-1>, <value-2>,. . .)];
INSERT INTO <table-name>
[(<column-name-1, column-name-2>,...)]
SELECT <column-name(s)>
FROM <table-name-2>
[WHERE <condition>];

Deleting Rows
DELETE FROM <table-name>
[WHERE condition-list];

Updating Rows
UPDATE <table-name / view-name>
SET <column-name-1 > = <value / expression>,
. . . . . .
<column-name-n> = <value expression>
[WHERE <condition>];

Aggregate Functions
SELECT aggregate function ([DISTINCT]<column-name>)
FROM <table-name> [WHERE <condition>];
Aggregate Functions:
AVG
SUM
MIN
MAX
COUNT (may be used with*)

Grouping
SELECT <column-name(s)>, <built-in function(argument)>
FROM <table-name>
[WHERE <condition>]
GROUP By <column-name(s)>
[HAVING <condition>]
[ORDER BY <column-name(s)> [ASC/DESC]];
Joining Tables
SELECT <column-name(s)>
FROM <table-name-1>,<table-name-2>[,<table-name-3>,. . .]
WHERE <join-condition>
[AND / OR <condition>]
[ORDER By <column-name(s)>];

SELECT <column-name(s)>
FROM <table-name-1> JOIN <table-name-2>
ON <table-name-1>.<column-name> = <table-name-2>.<column-name>
[WHERE <condition>]
[ORDER By <column-name(s)>];

2 of 4 Prepared by: Tsegay S.


Mekelle University-CS Department ICT352

SELECT <column-name(s)>
FROM <table-name-1> JOIN <table-name-2>
USING (<column-name>)
[WHERE <condition>]
[ORDER By <column-name(s)>];

Subqueries
Main SELECT <column-name(s)>
Query FROM <table-name>
WHERE <comparison operator / IN>
Sub (SELECT <Column-name>
Query FROM <table-name>
[WHERE <condition>]);

Multi-subquery operators: ANY and ALL


Can be used in place of IN if the operator in the where clause is
inequality operator (< or >).
From Subqueries
SELECT <column-name(s)>
FROM <table-name(s)>, (SELECT <Column-name>
FROM <table-name>
[WHERE <condition>])
[WHERE <condition>]);
Attribute list subqueries (Inline Subqueries)
SELECT <column-name> operator (SUBQUERY) AS alias-column-name
FROM <table-name>
Data Definition Language

Create a table.
CREATE TABLE <table-name>
(<column-name-1> <data-type>[<constraint>],
<column-name-2> <data-type>[<constraint>],
. . . . . . . . .
<column-name-n><data-type> [<constraint>]
[CONSTRAINT [<constraint-name>] <constraint>]);

Alter a table.
Add a new column.
ALTER TABLE <table-name>
ADD <column-name><data-type>[constraint];
Modify the attribute constraints of an existing column.
ALTER TABLE <table-name>
MODIFY <column-name> <attribute constraints>;
Add a new column.
ALTER TABLE <table-name>
DROP <column-name>;
Add a table constraint.
ALTER TABLE <table-name>
ADD CONSTRAINT <constraint-name> <constraint>;

Remove a table constraint.


ALTER TABLE <table-name>
DROP CONSTRAINT <constraint-name>;

3 of 4 Prepared by: Tsegay S.


Mekelle University-CS Department ICT352

Drop a table.
DROP TABLE <table-n
Create a view.
CREATE VIEW <view-name>
AS <SQL query statements>;
Drop a view.
DROP VIEW <view-name>;

More SQL Syntax

Union
query UNION query
Note: the two queries must be union compatible

Intersect
query INTERSECT query

Minus
query MINUS query

Note: if the DBMS you are working on doesn’t support Intersect and Minus
operations, you can use IN and NOT IN subqueries to obtain similar results.

Correlated Subqueries (Type-II)


)See examples in the textbook
EXISTS Subquery

SOME HELPFUL COMMANDS

To see all the tables created within your schema.


SELECT * FROM USER_TABLES;

To see all the views created within your schema.


SELECT * FROM USER_VIEWS;
To see all the constraints that you have created within your schema.
SELECT * FROM user_constraints;
To see the details of a given constraint.
SELECT * FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = <constraint-
name>;
To see the full structure of a table.
SELECT * FROM user_tab_columns WHERE table_name = <table-name>;
To make your changes to the database permanent without logging out.
COMMIT;

To undo your pending changes to the database.


ROLLBACK;

4 of 4 Prepared by: Tsegay S.

You might also like