Please note that the following blog post provides a summary view for what you need to get done (left column) and quick examples that illustrate how to do it in SQL (right column with SQL code in red). For more information please see the resources below:
- SQL Course 2. (2018). SQL Course 2: Interactive Online SQL Training. Retrieved from http://www.sqlcourse2.com/
- Mode Community. (2018). The SQL Tutorial for Data Analysis. Retrieved from https://community.modeanalytics.com/sql/tutorial/introduction-to-sql/
- Programmer Interview. (2018). Practice SQL Interview Questions and Answers. Retrieved from http://www.programmerinterview.com/index.php/database-sql/practice-interview-question-1/
Select |
SELECT * FROM dept |
Parentheses |
SELECT ename, sal, 12*(sal+100) FROM emp |
Column Alias |
SELECT ename, AS name, sal salary FROM emp |
Eliminating Duplicate Rows |
SELECT DISTINCT deptno FROM emp |
Selecting with WHERE |
SELECT ename, job, deptno FROM emp WHERE job=’Clerk’ |
Comparison Operators in WHERE |
SELECT ename, sal, comm FROM emp WHERE sal<=comm |
Between Operators in WHERE |
SELECT ename, sal, comm FROM emp WHERE sal BETWEEN 1000 AND 1500 |
Test Values in a list |
SELECT ename, empno, mgr FROM emp WHERE mgr IN (7902, 7566, 7788) |
Pattern Matching in WHERE
_ is one space character * % is trailing characters * * is all characters |
SELECT ename FROM emp WHERE ename LIKE ‘_A%’ |
Both conditions in WHERE are true |
SELECT ename, empno, job, sal FROM emp WHERE sal>=1100 AND job=’Clerk’ |
One or the other in WHERE are true |
SELECT ename, empno, job, sal FROM emp WHERE sal>=1100 OR job=’Clerk’ |
Not Operator in WHERE |
SELECT ename, empno, job, sal FROM emp WHERE job NOT IN (’Clerk’, ’Manager’, ’Analyst,) |
Sort rows |
SELECT ename, job, deptno, hiredate FROM emp ORDER BY hiredate |
Descending order sorting of rows |
SELECT ename, job, deptno, hiredate FROM emp ORDER BY hiredate DESC |
Table aliases |
SELECT e.empno, e.ename, e.deptno, d.deptno, d.loc FROM emp e, dept d WHERE e.deptno = d.deptno |