Syntax For SQL
Syntax For SQL
To connect:
Eg SQL> SELECT first_name AS name, last_name AS surname, email AS address FROM employees;
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
Comparison operators.
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;
Eg SQL> SELECT first_name, last_name, salary FROM employees WHERE salary BETWEEN 8000 AND
15000;
SQL> SELECT attribute*, attribute FROM entity WHERE attribute* operator 'item';
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);
SQL> select first_name, last_name, commission_pct from employees where commission_pct is null ;
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}
SQL> SELECT attribute*, attribute*, SUM(attribute) FROM employees GROUP BY attribute*, attribute*;
Use JOIN….BY
SQL> SELECT attribute, attribute, attribute FROM entity1 JOIN entity2 USING(attribute common between
the two entities);
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.
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}
SQL> ALTER TABLE [table name] ADD ([attribute] [data type] [constraint]);
Use DROP.
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', 'joshejok@gmail.com', '0771654786');
Eg SQL> insert into customer (customer_id, first_name, last_name, email, phone_number) values (1,
'Lawrence', 'Asizo', 'law@gmail.com', '0760567776');
Eg SQL> INSERT INTO customer (customer_id, first_name, last_name, email, phone_number) VALUES (3,
'Sarah', 'Butenga', 'butengsara@gmail.com', '0701664757');
SQL> UPDATE [table_name] SET column_name = ‘new value’ WHERE [attribte = position of the item];
Enter user-name: hr
Enter password:
Connected.
Grant succeeded.
Connected.