Está en la página 1de 2

Be careful when updating records .

If we had omitted the WHERE clause, it will u pdate the same values in all records of the table. Ex: UPDATE Persons SET Address='Nissestien 67', City='Sandnes' Be careful when Deleting records as The WHERE clause specifies which record or r ecords that should be deleted. If we omit the WHERE clause, all the records will be deleted. Ex: DELETE FROM table_name or DELETE * FROM table_name DELETE FROM Persons or DELETE * FROM Persons The TOP clause is used Ex: SELECT TOP SELECT SELECT to specify the number of records to return. number|percent column_name(s) FROM table_name TOP 2 * FROM Persons TOP 50 PERCENT * FROM Persons

SQL SELECT TOP Equivalent in MySQL SELECT column_name(s) FROM table_name LIMIT number Example: SELECT * FROM Persons LIMIT 5 SQL SELECT TOP Equivalent in Oracle SELECT column_name(s) FROM table_name WHERE ROWNUM <= number Example: SELECT * FROM Persons WHERE ROWNUM <=5 The LIKE operator is used to search for a specified pattern in a column. SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern SELECT * FROM Persons WHERE City LIKE 's%' SELECT * FROM Persons WHERE City NOT LIKE '%tav%' SQL wildcards can be used when searching for data in a database & must be used w ith the LIKE operator. SELECT * FROM Persons WHERE City LIKE 'sa%' SELECT * FROM Persons WHERE City LIKE '%nes%' SELECT * FROM Persons WHERE FirstName LIKE '_la' SELECT * FROM Persons WHERE LastName LIKE 'S_end_on' SELECT * FROM Persons WHERE LastName LIKE '[bsp]%' SELECT * FROM Persons WHERE LastName LIKE '[!bsp]%' The IN operator allows you to specify multiple values in a WHERE clause. SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value 2,...) SELECT * FROM Persons WHERE LastName IN ('Hansen','Pettersen') The BETWEEN operator is used in a WHERE clause to select a range of data between two values. The values can be numbers, text, or dates. Note: The BETWEEN operator is treated differently in different databases ! SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen' SELECT * FROM Persons WHERE LastName NOT BETWEEN 'Hansen' AND 'Pettersen ' You can give a table or a column another name by using an alias using keyword AS alias_name SELECT column_name(s) FROM table_name AS alias_name SELECT column_name AS alias_name FROM table_name Types of JOIN you can use, and the differences between them are listed below. JOIN(INNER JOIN): Return rows when there is at least one match in both ta bles.

LEFT JOIN(LEFT OUTER JOIN): Return all rows from the left table, even if there are no matches in the right table. RIGHT JOIN: Return all rows from the right table, even if there are no ma tches in the left table. FULL JOIN: Return rows when there is a match in one of the tables. INNERJOIN: The INNER JOIN keyword return rows when there is at least one match i n both tables. SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_n ame1.column_name=table_name2.column_name SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName LEFT OUTER JOIN: The LEFT JOIN keyword returns all rows from the left table (tab le_name1), even if there are no matches in the right table (table_name2). SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_na me1.column_name=table_name2.column_name Ex: SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Pers ons LEFT JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName RIGHT JOIN: The RIGHT JOIN keyword returns all the rows from the right table (ta ble_name2), even if there are no matches in the left table (table_name1). EX: SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_n ame1.column_name=table_name2.column_name SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName The RIGHT JOIN keyword returns all the rows from the right table (Orders ), even if there are no matches in the left table (Persons). FULL JOIN: The FULL JOIN keyword return rows when there is a match in one of the tables. SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_na me1.column_name=table_name2.column_name SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons FULL JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName

También podría gustarte