SYNTAX FOR SQL.
To connect:
SQL> CONNECT hr/hr;
To view the tables(entity) available :
All records in the table.
SQL> SELECT * FROM tab;
To show the attributes in an entity(table)
Structure of the table.
SQL> DESCRIBE [the entity];
e.g SQL> DESCRIBE employees;
To view all details in an entity:
SQL> SELECT * FROM [entity];
E.g. SQL> SELECT * FROM employees;
To view specific details in an entity:
SQL> SELECT attribute, attribute, attribute FROM entity;
Eg SQL> SELECT first_name, last_name, email FROM employees;
To change the letter case(capital or small letters):
UPPER is for capital lettrs and LOWER is for small letters.
SQL> SELECT UPPER(attribute), LOWER(attribute), LOWER(attribute) FROM entity;
Eg SQL> SELECT UPPER(first_name), LOWER(last_name), LOWER(email) FROM employees;
To rename the heading:
NN represents new name.
SQL> SELECT attribute AS NN, attribute AS NN, attribute AS NN FROM entity;
Eg SQL> SELECT first_name AS name, last_name AS surname, email AS address FROM employees;
To sort data either in ascending or descending order:
Ascending as ASC and descending as DESC.
SQL> SELECT attribute*, attribute FROM entity ORDER BY attribute* {that you are using to sort or
column number} DESC or ASC;
Eg SQL> SELECT first_name, last_name FROM employees ORDER BY first_name DESC; or
Eg SQL> SELECT first_name, last_name FROM employees ORDER BY 1 ASC;
Comparison operators.
We use WHERE followed by =, >, <, =<, =>, !=
SQL> SELECT attribute, attribute, attribute* FROM entity WHERE attribute* OPERATOR FIGURE or
WORD;
Eg SQL> SELECT first_name, last_name, salary FROM employees WHERE salary > 7000;
Eg SQL> SELECT first_name, last_name, department_id FROM employees WHERE department_id = 100;
USING BETWEEN (must have a range):
Eg SQL> SELECT first_name, last_name, salary FROM employees WHERE salary BETWEEN 8000 AND
15000;
For specific values:
Eg SQL> SELECT first_name, last_name, department_id FROM employees WHERE department_id IN
(100, 50);
To return specific items.{condition is case sensitive}
SQL> SELECT attribute*, attribute FROM entity WHERE attribute* operator 'item';
Eg SQL> SELECT first_name, last_name FROM employees WHERE first_name = 'John';
SQL> SELECT first_name, last_name FROM employees WHERE first_name LIKE 'J%'; { starts with J…}
SQL> SELECT first_name, last_name FROM employees WHERE first_name like '%n'; {ends with …n}
SQL> SELECT first_name, last_name from employees WHERE first_name IN ('John', 'Peter'); {both John
and Peter only}
SQL> SELECT first_name, last_name, salary FROM employees WHERE first_name LIKE 'J%' AND salary >
5000; { for two conditions}
SQL> SELECT first_name, last_name, salary FROM employees WHERE first_name LIKE 'Ja%'; {starts with
Ja…}
SQL> SELECT first_name, last_name, salary FROM employees WHERE first_name LIKE '_a%'; { second
letter must be a}
SQL> SELECT first_name, last_name, salary, department_id FROM employees WHERE last_name LIKE
'%a' AND department_id IN (30, 100);
To know the number of characters.
SQL> SELECT LENGTH(attribute) FROM entity;
Eg SQL> SELECT LENGTH(last_name) FROM employees;
To return items which have specify number of characters:
SQL> SELECT attribute* FROM entity WHERE LENGTH(attribute*) COMPARISON FIGURE;
Eg SQL> SELECT last_name FROM employees WHERE LENGTH(last_name) >= 8;
SQL> select first_name, last_name, commission_pct from employees where commission_pct is null ;
To give values to items that are empty.
Use NVL
SQL> SELECT attribute, NVL (attribute*, FIGURE) FROM employees WHERE attribute* IS NULL;
Eg SQL> SELECT last_name, NVL (commission_pct, .2) FROM employees WHERE commission_pct IS
NULL; {adds .2 to those who didn’t have}
Eg SQL> select first_name, last_name, NVL (commission_pct, .2) from employees ;
To find the sum, average, minimum, maximum, count.
If attribute is only one,
SQL> SELECT SUM(attribute) FROM entity;
Eg SQL> SELECT SUM(salary) FROM employees;
If attributes are more than one, use GROUP BY,
SQL> SELECT attribute*, attribute*, SUM(attribute) FROM employees GROUP BY attribute*, attribute*;
Eg SQL> SELECT department_id, job_id, SUM(salary) FROM employees GROUP BY department_id,
job_id;
To return data from different entities.
Use JOIN….BY
INNER JOIN { returns data with matching rows}
OUTTER JOIN{ returns data without matching rows}
Eg using entities: employees and departments:
SQL> SELECT attribute, attribute, attribute FROM entity1 JOIN entity2 USING(attribute common between
the two entities);
Eg SQL> SELECT first_name, last_name, department_name FROM employees JOIN departments
USING(department_id);
SQL> SELECT first_name, last_name, department_name FROM employees INNER JOIN departments
USING(department_id);
SQL> SELECT department_name, first_name, last_name, job_title FROM departments JOIN employees
USING(department_id) JOIN jobs USING(job_id);
To put data from two columns into a single column or add sentence to data in soecific column.
Use CONCAT.
Add sentence to data in soecific column.
SQL> SELECT attribute, CONCAT(attribute, 'sentence to be added’) FROM entity;
Eg SQL> SELECT first_name, CONCAT(email, '@gmail.com') FROM employees;
To put data from two columns into a single column.
SQL> SELECT CONCAT(concat(attribte, ' '), attribute) FROM entity;
Eg SQL> SELECT CONCAT(first_name, last_name), LOWER(CONCAT(email, 'gmail.com')) AS Email FROM
employees; {returns data without spacing, email in lower case and to rename it}
SQL> SELECT CONCAT(CONCAT(first_name, ' '), last_name), LOWER(CONCAT(email, 'gmail.com')) AS
Email FROM employees;{ returns data with spacing}
To make figures uniform{ try it out and you will understand}
SQL> SELECT first_name, LPAD(salary, 5, '*') FROM employees; {puts uniformity on left side}
SQL> SELECT first_name, RPAD(salary, 5, '*') FROM employees; {puts uniformity on right side}
To create entity[table], insert attritubes and insert data.
Data types; VARCHAR2, NUMBER, FLOAT, TIMESTAMP, DATE, INTEGER…..
Constraints; PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, NULL……
SQL> CREATE TABLE entity name (column_name data_type(number of characters) constraint)…….;
Eg SQL> CREATE TABLE department(department_id VARCHAR2(5) PRIMARY KEY, department_name
VARCHAR2(15) NOT NULL, location VARCHAR2(20) NOT NULL);
To add an attribute to an already existing table.
Use ALTER.
SQL> ALTER TABLE [table name] ADD ([attribute] [data type] [constraint]);
To delete an attribute from an already existing table.
Use DROP.
SQL> ALTER TABLE [table name] DROP COLUMN attribute;
SQL> ALTER TABLE Customer DROP column employee_id;
To add data into the entity.
SQL> INSERT INTO entity ( attribute1, attribute2, attribute3) VALUES (‘value1’, ‘value 2’, ‘value3’);
Eg SQL> INSERT INTO customer (customer_id, first_name, last_name, email, phone_number) VALUES (2,
'Joshua', 'Ejok', '[email protected]', '0771654786');
Eg SQL> insert into customer (customer_id, first_name, last_name, email, phone_number) values (1,
'Lawrence', 'Asizo', '[email protected]', '0760567776');
Eg SQL> INSERT INTO customer (customer_id, first_name, last_name, email, phone_number) VALUES (3,
'Sarah', 'Butenga', '[email protected]', '0701664757');
To correct data already existing entity.
SQL> UPDATE [table_name] SET column_name = ‘new value’ WHERE [attribte = position of the item];
Eg SQL> UPDATE customer SET phone_number = ‘+256771653427’ WHERE customer_id = 1;
To delete data from already existing entity.
SQL> DELETE FROM [table_name] WHERE [attribte = position of the item];
Eg SQL> DELETE FROM customer WHERE customer_id = 1;
SQL> connect as sysdba;
Enter user-name: hr
Enter password:
Connected.
SQL> create user Aimers identified by aimers;
User created.
SQL> grant connect, resource to Aimers;
Grant succeeded.
SQL> connect Aimers/aimers;
Connected.