Explore model answers categorized by subjects like Java, HTML, DBMS, and more.
HAVING cause is using for the show groups satisfying a criteria, SELECT * FROM employer GROUP BY dsgn HAVING pay > 10000;
IN clause uses when we would like to get results if a field has any of the given value :SELECT * FROM employer where name IN (\'Ramesh\',\'Ram\');
To count number of records in the table .For example we want to know How many employers are in the table :SELECT COUNT(*) FROM employer;
To retrieve a row with columns having a particular pattern. For the Example pay of all those employees whose name starts with A :
SELECT pay FROM Employer WHERE name like \'A%\';
To retrieve selected columns of a particular row:SELECT pay FROM employer WHERE name = \'Ram\';
When i would like to insert the data then we use to the \'INSERT INTO\' command followed by table name and values to be inserted.
> If we need to insert a row with values for all columns, then use the following command :INSERT INTO employer VALUES(\'Ram\',\'MGR\',25,25000);
> If we need to insert values for selected columns only, then we need to specify those column names also in the command as shown :INSERT INTO employer (NAME) VALUES (\'Ramesh\');
For the create Table firstly we use \'CREATE TABLE\' command with name and data-type of each table field. We can also specify PRIMARY KEY and any other constraint like NOT NULL.
For example:CREATE TABLE employer(NAME VARCHAR(80) PRIMARY NOT NULL,DSGN VARCHAR(5),AGE INTEGER,PAY INTEGER);
To retrieve all columns of a particular row we can use where clause in sqlSELECT * FROM employer where name = \'Ramesh\';
To retrieve few columns of all rows:SELECT name FROM employer;
We have a two ways for updating the table, Which is shown here :
Use UPDATE command with SET and name-value pairs like:UPDATE Employer SET pay=40000;
For updating a particular row, Then We use WHERE clause in UPDATE command like :UPDATE Employer SET pay = 25000 WHERE NAME= \'Ram\';
When we would like to get the sorted listed of the data with respect to any columns , Then we can use ORDER BY clause .
For the example we can shortlist of the table with respect to columns Name as shown :SELECT * FROM employer ORDER BY name;
When we would like to see our table then we Use SELECT command to retrieve the data.SELECT * FROM employer;
Use AND/OR in WHERE clause to retrieve data based on multiple condition.SELECT * FROM employer WHERE name LIKE \'A%\' AND pay > 10000;
We can also delete all records using TRUNCATE command such asTRUNCATE TABLE employer;
For delete all records, We can use :DELETE FROM Employer;
For delete a particular record, use DELETE command with WHERE clause like:DELETE * FROM employer WHERE name=\'Ram\';
For this query we can use AS command as defined below:SELECT NAME,DSGN FROM Employer AS employees;
When we add a columns in the table we can done through ALTER command like :ALTER TABLE employer ADD exp INTEGER;
When we want to be do for like this then we can use ALTER command with DROP like this :ALTER TABLE employer DROP exp;
When i wold like to drop the create table then we Use DROP TABLE command followed by table name.DROP TABLE employer;
Using the Min function, we can know the average value of a numeric column.SELECT MIN(SAL) employer;
When we like to drop Database then we use DROP DATABASE command followed by database name :DROP DATABASE emp;
Using the Max function, we can know the average value of a numeric column.SELECT MAX(SAL) employer;
Using the Avg function, we can know the average value of a numeric column.SELECT Avg(SAL) employer;
Distinct Statement is basically use when in the table some of the columns may contain duplicate values.
It is not a problem .some time we want to be these type of values like :
If we have a any table , and we wold like to know the name of those persons who are from same place. that time we use this statement :Syntax : SELECT DISTINCT column_name(s) FROM table_name
Example:SELECT DISTINCT City FROM employer;
We are using Top clause(oracle) because if we like to know some limit of rows see in the output or some %age of the table we would like to show then we use this clause like :
> If we would like to see select only the two first records in the table above :SELECT TOP 2 * FROM employer;
> If we would like to see select only 50% records in the table above :SELECT TOP 50 PERCENT * FROM employer;
Now we want to select the employer with a last name that starts with \"A\" or \"n\" or \"k\" from the \"employer\" table.SELECT * FROM employer WHERE LastName LIKE \'[ank]%\';
We want to select the employer with a first name that starts with any character, followed by \"Ka\" from the \"employer\" table.SELECT * FROM employer WHERE FirstName LIKE \'_ka\';
Now we want to select the employer with a last name that starts with \"R\", followed by any character, followed by \"end\", followed by any character, followed by \"on\" from the \"employer\" table.SELECT * FROM employer WHERE LastName LIKE \'R_end_on\';
This wildcard we use when we want to show which name who start from some special char like :
We want to select the persons living in a city that starts with \"Ka\" from the \"employer\" table.SELECT * FROM employer WHERE City LIKE \'ka%\';
SQL Alias is the basically use for the make a easier to the query, I mean when you have a very long or complex table names or column names. Then we can use this function like :
Assume we have a table called \"Employer\" and another table called \"Product_Orders\". We will give the table aliases of \"e\' an \"po\" respectively. Now we would like to list all the orders that \"Modi groups\" is responsible for :
SELECT Statement With Alias:SELECT po.OrderID, e.LastName, e.FirstName FROM Employer AS e ,Product_Orders AS p WHERE p.LastName=\'Modi\' WHERE p.FirstName=\'Groups\'
Now SELECT Statement with out Alias :SELECT Product_Orders.OrderID, Employer.LastName, Employer.FirstName FROM Employer, Product_Orders WHERE Employer.LastName=\'Modi\' WHERE Emloyer.FirstName=\'Groups\';
Now we can see that with alias query is so easy .
The INNER JOIN basically use for the return rows when there is at least one match in both tables.Syntax : SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name
The FULL JOIN keyword is basically use for returning rows when there is a match in one of the tables.Syntax :SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name
In some databases RIGHT JOIN is called RIGHT OUTER JOIN. The RIGHT JOIN keyword is basically use for the Returning to the all rows from the right table (table_name2), even if there are no matches in the left table (table_name1).Syntax :SELECT column_name(e) FROM table_name1RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
In some databases LEFT JOIN is called LEFT OUTER JOIN. The LEFT JOIN keyword is basically a use for the returning the all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).Syntax :SELECT column_name(s)FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
The ROUND() function is basically use for the round a numeric field to the number of decimals specified.Syntax : SELECT ROUND(column_name,decimals) FROM table_name
The NOW() function is using for the returning the current system date and time.Syntax : SELECT NOW() FROM table_name
The FORMAT() function is basically used to format how a field is to be displayed.Syntax : SELECT FORMAT(column_name,format) FROM table_name
The LEN() function is basically use for the returning the value of length of the value in a text field.Syntax : SELECT LEN(column_name) FROM table_name
The MID() function is basically use for the extract the characters from the txt fields.Syntax : SELECT MID(column_name,start[,length]) FROM table_name
The UNION operator is used to combine the result-set of two or more SELECT statements.Each SELECT statement within the UNION must have the same number of columns.
The columns must also have similar data types. Also the columns in each SELECT statement must be in the same order.Syntax for UNION : SELECT column_name(e) FROM Table_name1 UNION SELECT column_name(e) FROM table_name2
SQL UNION ALL SyntaxSELECT column_name(e) FROM table_name1 UNION ALL SELECT column_name(e) FROM table_name2
The LCASE() function is basically use for the converts the field to Lowercase.Syntax : SELECT UCASE(column_name) FROM table_name
The LAST() function is mainly use for the returning the Last value of the selected column.Syntax : SELECT FIRST(column_name) FROM table_name
The UCASE() function is basically use for the convert the value of a field to uppercase.Syntax: SELECT UCASE(column_name) FROM table_name
DATABASE statement is basically crate the CREATE DATABASE Command.Syntax : CREATE DATABASE database_name
The SQL SELECT INTO statement basically for the use in many fields and It can be used to create backup copies of tables.
The SELECT INTO statement is use for the selects data from one table and It can also be inserts it into a different table.
The SELECT INTO statement is mostly use for the backup copies of tables.Syntax : SELECT * INTO new_table_name [IN externaldatabase] FROM old_tablename
We can select only the columns we want into the new table :SELECT column_name(e)I NTO new_table_name [IN externaldatabase] FROM old_tablename
The CREATE INDEX statement is basically use for the create indexes in tables. Its mainly Indexes allow the database application to find data fast, without reading the whole table.Syntax : CREATE INDEX index_name ON table_name (column_name)
When we would like to Unique Index then syntax,but its have not allowed duplicate :Syntax : CREATE UNIQUE INDEX index_name ON table_name (column_name)
The FIRST() function is used to the returning the first value of the selected column.Syntax : SELECT FIRST(column_name) FROM table_name
Not NUll Constraints is basically means that we can\'t insert a new record, or also cant be update a record without adding a value to this field. The NOT NULL constraint enforces a column to NOT accept NULL values. The NOT NULL constraint enforces a field to always contain a value.
For the example: The following SQL enforces the \"E_Id\" column and the \"LastName\" column to not accept NULL values :CREATE TABLE Employer(E_Id int NOT NULL,LastName varchar(255)NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255))
To get the sum of a column .For example we want to know total pay to be paid :SELECT SUM(PAY) FROM employer;
To group the results, use GROUP BY as in following:SELECT * FROM employer GROUP BY deprt;