You are on page 1of 6

Mizan Aman College of Health Science

Department of HIT
SQL statements and syntax
Prepared by Mr. Miftah Temam (Health Informatics)

SQL Quick Reference From

SQL Statement Syntax Comment


Create database database_name;
Create database
Example: create database Hospital;
Create table table_name (
column_name1 data_type (size),
column_name2 data_type (size),
column_name3 data_type (size));
Example:
Create table create table patient(
MRN numeric(6),
First_Name varchar(20),
Last_Name varchar(30),
Age numeric(2),
Sex char(1));
Create table table_name ( Primary key can be
column_name1 data_type (size), specified when a table
column_name2 data_type (size), is created (with the
column_name3 data_type (size), CREATE TABLE
constraint table_name_pk primary key (column_name)); statement)
Example:
Primary key
create table patient(
MRN numeric(6),
First_Name varchar(20),
Last_Name varchar(30),
Age numeric(2),
Sex char(1),
constraint patient_pk primary key (MRN));

1
June 2022 GC.
Mizan Aman College of Health Science
Department of HIT
SQL statements and syntax
Prepared by Mr. Miftah Temam (Health Informatics)

Create table table_name ( Foreign key can be


column_name1 data_type (size), specified when a table is
column_name2 data_type (size), created (with the
column_name3 data_type (size), CREATE TABLE
constraint table_name_pk primary key (column_name), statement)
constraint table_name_fk foreign key
(reference_column_name)references
table_name(reference_column_name);
Foreign key Example
create table doctor(
doc_ID varchar(4), Column from the
First_Name varchar(25), referencing table
Last_Name varchar(25),
MRN numeric(6),
constraint doctorpk primary key(doc_ID),
constraint doctorfk foreign key(MRN) references
patient(MRN));
The NOT NULL constraint
Example: enforces a column to
create table patient( NOT accept NULL values.
NOT NULL MRN numeric(6)NOT NULL,
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Age numeric(2) NOT NULL,
Sex char(1));
ALTER TABLE table_name ADD column_name
datatype(size);
ALTER TABLE
(Add column) Example:
alter table patient add Telephone_Number
numeric(10);
2
June 2022 GC.
Mizan Aman College of Health Science
Department of HIT
SQL statements and syntax
Prepared by Mr. Miftah Temam (Health Informatics)

Some database systems


ALTER TABLE table_name DROP COLUMN column_name; don't allow deleting a
ALTER TABLE
(Delete column) column.
Example:
alter table patient drop column Registered_Date;
The second form is
It is possible to write the INSERT INTO statement recommended.
in two forms.
It is also possible to only
INSERT INTO table_name VALUES (value1, value2, add data in specific
value3);
columns.
Example:
insert into doctor
INSERT INTO values('Doc1','Abebe','Feleke');
OR

INSERT INTO table_name (column1, column2, column3)


VALUES (value1, value2, value3);

Example:
insert into doctor (ID, First_Name,
Last_Name)values 'Doc1','Abebe','Feleke');

UPDATE table_name SET column1=value WHERE The WHERE clause


some_column= value; specifies which record or
UPDATE records that should be
(updating existing Example: updated. If you omit the
records) update patient set Telephone_Number=0909090909 WHERE clause, all
where MRN=908765;
records will be updated!

3
June 2022 GC.
Mizan Aman College of Health Science
Department of HIT
SQL statements and syntax
Prepared by Mr. Miftah Temam (Health Informatics)

update patient set First_Name='Wesenu' where Multiple columns can be


Middle_Name='Lemma'; updated within one
UPDATE statement
separating by comma.
Deleting a single record
DELETE FROM table_name WHERE
some_column=some_value;

Example:
DELETE
delete from patient where MRN=908765;
(deleting rows)
Deleting all records
DELETE FROM table_name;
Example:
delete from patient;

Select column_name1, column_name2, column_name3


from table_name;
Example:
select CustomerId, LastName, FirstName from
CUSTOMER;

SELECT OR

Select * from table_name;

Example:
select * from CUSTOMER;

4
June 2022 GC.
Mizan Aman College of Health Science
Department of HIT
SQL statements and syntax
Prepared by Mr. Miftah Temam (Health Informatics)

Select * from table_name ORDER BY column_name; If you want the data to


appear in a specific order
Example: you need to use the
select * from CUSTOMER order by LastName; “order by” keyword.
ORDER BY select * from CUSTOMER order by LastName desc; Desc= Descending
select * from CUSTOMER order by LastName asc; order
You may also sort by several columns Asc= Ascending
Example: order
select * from CUSTOMER order by Address, LastName;
The DISTINCT keyword
Select distinct column_name from table_name; can be used to return
SELECT DISTINCT only distinct (different)
Example: values.
select distinct Sex from patient;
select distinct FirstName from CUSTOMER;
Example: The WHERE clause is
select * from patient where Age=24; used to extract only
WHERE clause select * from customer where First_Name=’Abebe’; those records that fulfill
a specified criterion
SELECT column_name(s) FROM table_name WHERE The LIKE operator is used
column_name LIKE ‘pattern’; to search for a specified
Examples: pattern in a column.
select * from CUSTOMER where LastName like 'J%';
LIKE operator
select * from patient where Sex like'F';
select MRN, FirstName,LastName where Address
like’Adis Abeba’;
select * from CUSTOMER where LastName like '%a%';
SELECT column_name(s) FROM table_name WHERE The IN operator allows
column_name IN (value1,value2,...); you to specify multiple
IN operator Examples: values in a WHERE clause
select * from patient where Age in (23,29);

5
June 2022 GC.
Mizan Aman College of Health Science
Department of HIT
SQL statements and syntax
Prepared by Mr. Miftah Temam (Health Informatics)

SELECT column_name(s) FROM table_name WHERE column_name The BETWEEN operator


BETWEEN value1 AND value2; selects a range of data
BETWEEN operator Example:
between two values. The
select * from patient where Age between 25 and 30; values can be
numbers, text, or dates.
Example: AND operator displays a
record if both the first
AND operator select * from patient where First_Name='Abebe' and condition and the second
Last_Name='Kebede';
condition is true.
Example: The OR operator displays
a record if either the first
OR operator select * from patient where First_Name='Abebe' OR condition or the second
Last_Name='Kebede';
condition is true

SELECT TOP number|percent column_name(s) FROM


table_name;
SELECT TOP clause Example: select top 2*from patient;
You can also specify in percent:
Example: select top 60 percent *from patient;

MINIMUM Examples:
select min (Age) from patient ;
MAXIMUM
select max (Age) from patient ;
AVERAGE
select avg(age) from patient;
COUNT select count(First_Name) from patient;
SUM select sum(Salary) from Doctor;

6
June 2022 GC.

You might also like