B: O: D: / M: A: + S: - : Precedence: BODMAS
B: O: D: / M: A: + S: - : Precedence: BODMAS
B: O: D: / M: A: + S: - : Precedence: BODMAS
Syntax of SQL SELECT Statement: SELECT column_list FROM table-name [WHERE Clause] [GROUP BY clause] [HAVING clause] [ORDER BY clause]; Syntax of SQL SELECT Statement to combine two columns: SELECT first_name || ' ' || last_name FROM employee; Syntax of SQL SELECT Statement to provide Alias SELECT first_name || ' ' || last_name AS emp_name FROM employee;
5)SQL TRUNCATE Statement The SQL TRUNCATE command is used to delete all the rows from the table and free the space containing the table.
Syntax to TRUNCATE a table:
so be careful while using RENAME command. When a table is dropped all the references to the table will not be valid.
SQL ORDER BY
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or descending order. Oracle sorts query results in ascending order by default.
Syntax for using SQL ORDER BY clause to sort data is: SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1 [, column2, .. columnN] [DESC]]; SQL GROUP BY Clause The SQL GROUP BY Clause is used along with the group functions to retrieve data grouped according to one or more columns. For Example: If you want to know the total amount of salary spent on each department, the query would be: SELECT dept, SUM (salary) FROM employee GROUP BY dept; SQL HAVING Clause Having clause is used to filter data based on the group functions. This is similar to WHERE condition but is used with group functions. Group functions cannot be used in WHERE Clause but can be used in HAVING clause. For Example: If you want to select the department that has total salary paid for its employees more than 25000, the sql query would be like; SELECT dept, SUM (salary) FROM employee GROUP BY dept HAVING SUM (salary) > 25000
SQL LIKE Operator The LIKE operator is used to list all rows in a table whose column values match a specified pattern. It is useful when you want to search rows to match a specific pattern, or when you do not know the entire value. For this purpose we use a wildcard character '%'. For example: To select all the students whose name begins with 'S' SELECT first_name, last_name FROM student_details WHERE first_name LIKE 'S%';
SQL BETWEEN ... AND Operator
The operator BETWEEN and AND, are used to compare data for a range of values. For Example: to find the names of the students between age 10 to 15 years, the query would be like, SELECT first_name, last_name, age FROM student_details WHERE age BETWEEN 10 AND 15; SQL IN Operator: The IN operator is used when you want to compare a column with more than one value. It is similar to an OR condition.
For example: If you want to find the names of students who are studying either Maths or Science, the query would be like, SELECT first_name, last_name, subject FROM student_details WHERE subject IN ('Maths', 'Science'); SQL IS NULL Operator A column value is NULL if it does not exist. The IS NULL operator is used to display all the rows for columns that do not have a value. For Example: If you want to find the names of students who do not participate in any games, the query would be as given below SELECT first_name, last_name FROM student_details WHERE games IS NULL
SQL Primary key: This constraint defines a column or combination of columns which uniquely identifies each row in the table. Syntax to define a Primary key at column level: column name datatype [CONSTRAINT constraint_name]
[CONSTRAINT
constraint_name]
PRIMARY
KEY
(column_name1,column_name2,..)
SQL Foreign key or Referential Integrity : This constraint identifies any column referencing the
PRIMARY KEY in another table. It establishes a relationship between two columns in the same table or between different tables. For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One or more columns can be defined as Foreign key. Syntax to define a Foreign key at column level: [CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name) Syntax to define a Foreign key at table level: [CONSTRAINT constraint_name] FOREIGN KEY(column_name) REFERENCES referenced_table_name(column_name);
SQL Not Null Constraint : This constraint ensures all rows in the table contain a definite value for the column which is specified as not null. Which means a null value is not allowed. Syntax to define a Not Null constraint: [CONSTRAINT constraint name] NOT NULL
This constraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can have a null value but the values cannot be duplicated. Syntax to define a Unique key at column level: [CONSTRAINT constraint_name] UNIQUE Syntax to define a Unique key at table level: [CONSTRAINT constraint_name] UNIQUE(column_name)
SQL COUNT (): This function returns the number of rows in the table that satisfies the condition specified in the WHERE condition. If the WHERE condition is not specified, then the query returns the total number of rows in the table. For Example: If you want the number of employees in a particular department, the query would be:
SQL DISTINCT(): This function is used to select the distinct rows. For Example: If you want to select all distinct department names from employee table, the query would be:
SELECT DISTINCT dept FROM employee; To get the count of employees with unique name, the query would be: SELECT COUNT (DISTINCT name) FROM employee;
SQL MAX(): This function is used to get the maximum value from a column. To get the maximum salary drawn by an employee, the query would be: SELECT MAX (salary) FROM employee;
SQL MIN(): This function is used to get the minimum value from a column. To get the minimum salary drawn by an employee, he query would be: SELECT MIN (salary) FROM employee;
SQL AVG(): This function is used to get the average value of a numeric column. To get the average salary, the query would be SELECT AVG (salary) FROM employee;
SQL SUM(): This function is used to get the sum of a numeric column To get the total salary given out to the employees, SELECT SUM (salary) FROM employee;
SQL Joins
SQL Joins are used to relate information in different tables. A Join condition is a part of the sql query that retrieves rows from two or more tables. A SQL Join condition is used in the SQL WHERE Clause of select, update, delete statements. The Syntax for joining two tables is: SELECT col1, col2, col3... FROM table_name1, table_name2 WHERE table_name1.col2 = table_name2.col1; SQL Joins can be classified into Equi join and Non Equi join. 1) SQL Equi joins It is a simple sql join condition which uses the equal sign as the comparison operator. Two types of equi joins are SQL Outer join and SQL Inner join.
For example: You can get the information about a customer who purchased a product and the quantity of product.
2) SQL Non equi joins It is a sql join condition which makes use of some comparison operator other than the equal sign like >, <, >=, <= 1) SQL Equi Joins: An equi-join is further classified into two categories: a) SQL Inner Join b) SQL Outer Join a) SQL Inner Join: All the rows returned by the sql query satisfy the sql join condition specified. For example: If you want to display the product information for each order the query will be as given below. Since you are retrieving the data from two tables, you need to identify the common column between these two tables, which is theproduct_id. The query for this type of sql joins would be like, SELECT order_id, product_name, unit_price, supplier_name, total_units FROM product, order_items WHERE order_items.product_id = product.product_id;
The columns must be referenced by the table name in the join condition, because product_id is a column in both the tables and needs a way to be identified. This avoids ambiguity statement. The number of join conditions is (n-1), if there are more than two tables joined in a query where 'n' is the number of tables involved. The rule must be true to avoid Cartesian product. We can also use aliases to reference the column name, then the above query would be like, SELECT o.order_id, p.product_name, p.unit_price, p.supplier_name, o.total_units FROM product p, order_items o WHERE o.product_id = p.product_id; b) SQL Outer Join: This sql join condition returns all rows from both tables which satisfy the join condition along with rows which do not satisfy the join condition from one of the tables. The sql outer join operator in Oracle is ( + ) and is used on one side of the join condition only. The syntax differs for different RDBMS implementation. Few of them represent the join conditions as "sql left outer join", "sql right outer join". If you want to display all the product data along with order items data, with null values displayed for order items if a in using the columns in the SQL SELECT
product has no order item, the sql query for outer join would be as shown below: SELECT p.product_id, p.product_name, o.order_id, o.total_units FROM order_items o, product p WHERE o.product_id (+) = p.product_id; SQL Self Join: A Self Join is a type of sql join which is used to join a table to itself, particularly when the table has a FOREIGN KEY that references its own PRIMARY KEY. It is necessary to ensure that the join statement defines an alias for both copies of the table to avoid column ambiguity. The below query is an example of a self join, SELECT a.sales_person_id, a.name, a.manager_id, b.sales_person_id, b.name FROM sales_person a, sales_person b WHERE a.manager_id = b.sales_person_id;
SQL Views A VIEW is a virtual table, through which a selective portion of the data from one or more tables can be seen. Views do not contain data of their own. They are used to restrict access to the database or to hide data complexity. A view is stored as a SELECT statement in the database. DML operations on a view like INSERT, UPDATE, DELETE affects the data in the original table upon which the view is based.
The Syntax to create a sql view is CREATE VIEW view_name AS SELECT column_list FROM table_name [WHERE condition];
Syntax to create Index: CREATE INDEX index_name ON table_name (column_name1,column_name2...); Syntax to create SQL unique Index: CREATE UNIQUE INDEX index_name ON table_name (column_name1,column_name2...);