Select:
Command:
SELECT ColumnName1, ColumnName2, …FROM Table1
if we want to select all the data:
SELECT * FROM Weather
Insert:
whele inserting date and text we have to use single colen''
command:
1)INSERT INTO tablename (empid,empname,salary)
VALUES (1, 'chandra', 25000)
2)INSERT INTO Weather VALUES ('Los Angeles', 20, '10/10/2005')
Where:
Command:
SELECT *
FROM Weather
WHERE AverageTemperature > = 20 OR City <> 'New York'
Distinct(for selecting the unique data from the table)
Command:
SELECT DISTINCT Column1
FROM Table1
UPDATE:
Command:
UPDATE Table1 SET Column1 = Value1, Column2 = Value2, …
Delete:
Command:
1)DELETE FROM Table1
Truncate:
it is used only to delete all rows in a table
Command:
TRUNCATE TABLE Weather
SQL ORDER BY :
Command:
in ascending:
SELECT * FROM emp ORDER BY empid
indecending:
SELECT * FROM emp ORDER BY empname DESC
SQL aggregate :functions are used to sum, count, get the average, get the minimum and get the maximum values from a column or from a sub-set of column values.
Command:
1)for average:SELECT AVG(salary) FROM emp
2)for count:SELECT count(*) FROM emp
3)average for perticular name or else the command is:
SELECT AVG(AverageTemperature)
FROM emp
WHERE empname = 'sarat'
4)To get the minimum value from a numeric table colum the command is :
Select Min(salary) from emp
5)To get the maximum value from a numeric table colum the command is :
Select Max(salary) from emp
6)To get the sum of a numeric table comum the command is:
Select sum(salary) from emp
The SQL GROUP BY clause is used along with the SQL aggregate functions and specifies the groups where selected rows are placed. WHEN one or more aggregate functions are presented in the SQL SELECT column list, the SQL GROUP BY clause calculates a summary value for each group
Command:
SELECT empid, AVG(salary)
FROM emp
GROUP BY empid
The SQL HAVING :keyword provides a search condition for a group or aggregate. The SQL HAVING clause works together with the SQL SELECT clause. The SQL HAVING clause is somewhat similar to the SQL WHERE clause, because it specifies a search condition.
There is one important difference between SQL HAVING and SQL WHERE clauses. The SQL WHERE clause condition is tested against each and every row of data, while the SQL HAVING clause condition is tested against the groups and/or aggregates specified in the SQL GROUP BY clause and/or the SQL SELECT column list.
It is important to understand that if a SQL statement contains both SQL WHERE and SQL HAVING clauses the SQL WHERE clause is applied first, and the SQL HAVING clause is applied later to the groups and/or aggregates.
Command:
SELECT empname, AVG(salary)
FROM emp
GROUP BY empname
HAVING AVG(salary) > 25000
Now we r entering in to the key Phase i.e joints it's really very important in praticall applications and keep some key attention aver here:
Joins:
Command:
SELECT emp.empname, emp.empid, emp.salary,empdetails.hobbies,empdetails.wifename
FROM emp,empdetails
WHERE emp.empname = empdetails.empname;
we can also use a object for our tables the example is as fallows:
Command:
select distinct e.empname,d.wifename from emp e,empdetails d where e.empname=d.empname
Explanation:her e is the object of emp and d is the object of empdetails
UNIONS:
The SQL UNION clause merges the results of two or more SELECT SQL queries into one result set. When using SQL UNION, all the SQL expressions participating in the UNION must have the same structure (they have to have the same number of columns and same or compatible data types). You have to keep the column order of all unionized SQL SELECT expressions uniform, otherwise you’ll get an error.
Here is an example of SQL UNION statement:
SELECT empid, empname, salary
FROM emp
UNION
SELECT empid, empname, salary
FROM emp1
Sunday, September 27, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment