Posts

Showing posts with the label SQL

SQL : Joins

Image
Introduction: A join is used to combine rows from multiple tables. A join is performed whenever two or more tables is listed in the FROM clause of an SQL statement. There are different kinds of joins. Let's take a look at a few examples. Inner Join (simple join) Chances are, you've already written an SQL statement that uses an inner join. It is the most common type of join. Inner joins return all rows from multiple tables where the join condition is met. For example SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_dateFROM suppliers, ordersWHERE suppliers.supplier_id = orders.supplier_id; This SQL statement would return all rows from the suppliers and orders tables where there is a matching supplier_id value in both the suppliers and orders tables. Let's look at some data to explain how inner joins work: We have a table called suppliers with two fields (supplier_id and supplier_ name).It contains the following data: We have another table called orders wit

SQL : Select Query : Distint Clause

Introduction: The DISTINCT clause allows you to remove duplicates from the result set. The DISTINCT clause can only be used with select statements. The syntax for the DISTINCT clause is: SELECT DISTINCT columnsFROM tablesWHERE predicates; Example #1 Let's take a look at a very simple example. SELECT DISTINCT categoryFROM ebooks; This SQL statement would return all unique categories from the ebooks table. Example #2 The DISTINCT clause can be used with more than one field. For example: SELECT DISTINCT category, publisherFROM ebooks; This select statement would return each unique category and publisher combination. In this case, the distinct applies to each field listed after the DISTINCT keyword.

SQL : Select Query

Introduction: The SELECT query/statement allows to retrieve records from one or more tables in database. The syntax for the SELECT statement is: SELECT [ columns] FROM [tables] WHERE [predicates]; Example #1 Let's take a look at how to select all fields from a table. SELECT *FROM ebooks WHERE author = 'Azhar'; In our example, we've used * to signify that we wish to view all fields from the ebooks table whose author is 'Azhar'. Example #2 You can also choose to select individual fields as opposed to all fields in the table. For example: SELECT name, ISBN FROM ebooks WHERE ebookid > 1000; This select statement would return all ebooks' name and their respective ISBN values from the ebooks table where the ebookid value is greater than 1000. Example #3 You can also use the select statement to retrieve fields from multiple tables. SELECT category.name,ebooks.ebookid, ebooks.name FROM category,ebooks WHERE category.categoryid=ebooks.categoryid; The result set wo