SQL Questions
SQL Questions
SQL Questions
Answer: Structured Query Language (SQL) is a language that provides an interface to relational database
systems. In common usage SQL also encompasses DML (Data Manipulation Language), for INSERTs,
UPDATEs, DELETEs and DDL (Data Definition Language), used for creating and modifying tables and
other database structures.
Question: What is SELECT statement?
Answer: The SELECT statement lets you select a set of values from a table in a database. The values
selected from the database table would depend on the various conditions that are specified in the SQL query.
Question: How can you compare a part of the name rather than the entire name?
Answer:
Example
SELECT *
FROM
employees
WHERE
first_name LIKE '%ab%';
Would return a record set with records consisting name the sequence 'ab' in first_name.
Question: What is the INSERT statement?
Answer: The INSERT statement lets you insert information into a database.
Question: How do you delete a record from a database?
Answer: Use the DELETE statement to remove records or any particular column values from a database.
Question: How could I get distinct entries from a table?
Answer: The SELECT statement in conjunction with DISTINCT lets you select a set of distinct values
from a table in a database. The values selected from the database table would of course depend on the
various conditions that are specified in the SQL query.
Example
SELECT DISTINCT first_name
FROM
employees;
Question: How to get the results of a Query sorted in any order?
Answer: You can sort the results and return the sorted results to your program by using ORDER BY
keyword thus saving you the pain of carrying out the sorting yourself. The ORDER BY keyword is used for
sorting.
Example for ascending order
SELECT *
FROM
employees
ORDER BY first_name ASC;
SELECT *
FROM
employees
ORDER BY first_name ASC;
Implicit cursors
For SQL queries returning single row PL/SQL declares implicit cursors. Implicit cursors are simple
SELECT statements and are written in the BEGIN block (executable section) of the PL/SQL.
Implicit cursors are easy to code, and they retrieve exactly one row. PL/SQL implicitly declares
cursors for all DML (INSERT, UPDATE, and DELETE) statements.
Explicit Cursors
Explicit cursors are used in queries that return multiple rows. The set of rows fetched by a query is
called active set. The size of the active set meets the search criteria in the select statement. Explicit
cursor is declared in the DECLARE section of PL/SQL program.
Question: What are triggers? How to invoke a trigger on demand?
Answer:
Triggers are special kind of stored procedures that get executed automatically when an INSERT,
UPDATE or DELETE operation takes place on a table. Triggers can't be invoked on demand. They
get triggered only when an associated action (INSERT, UPDATE, and DELETE) happens on the
tables upon which they are defined. Triggers are generally used to implement business rules,
auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible,
use constraints for this purpose, instead of triggers, as constraints are much faster.
Question: What is a join and explain different types of joins.
Answer:
Joins are used in queries to explain how different tables are related. Joins also let you select data
from a table depending upon data from another table. Types of joins: INNER JOINs, OUTER JOINs,
CROSS JOINs.OUTER JOINs are further classified as LEFT OUTER JOINS, RIGHT OUTER
JOINS and FULL OUTER JOINS.
Question: What is a self join?
Answer:Self join is just like any other join, except that two instances of the same table will be joined in the
query.
Question: How can I eliminate duplicates values in a table?
Answer: SQL> DELETE FROM table_name A WHERE ROWID > (SELECT min(rowid) FROM
table_name B WHERE A.key_values = B.key_values);
Question: How can I change my Oracle password?
Answer: From SQL*Plus type: ALTER USER username IDENTIFIED BY new_password
Question: What are the most important DDL statements in SQL?
Answer: The most important DDL statements in SQL are:
* create table-creates a new database table
first_name
employees
department_id IN (10, 69, 50);