Posts Tagged SQL
“SQL inner joins the right way “
The right way
————————————-
select s.CompanyName
from Suppliers as s
where exists
(
select * from products p
inner join categories c on p.CategoryID = c.CategoryID
where c.CategoryName = “Seafood”
and p.SupplierID=s.SupplierID
)
—————————————-
The wrong way
————————————————
SELECT DISTINCT Suppliers.CompanyName
FROM Suppliers INNER JOIN (Categories INNER JOIN Products ON
Categories.CategoryID = Products.CategoryID) ON Suppliers.SupplierID =
Products.SupplierID
WHERE (((Categories.CategoryName)=”Seafood”))
————————————————
Add comment November 26, 2008
“SQL query to match the data in the passed comma sepearted string”
There are several occasions when we have to match whether a passed collection of id is there in the database or not , in this case we can design the SQL in such a fashion that it checks for the id = (1,2,3), so for that we need to use IN operator.
Eg:
select * from table_name where table_name.id IN (array of integer or anything else)
Now here the array of integer or anything else should be in a comma separated manner like (1,2,3) or (‘asa’,’sa’,’sasa’)
Add comment March 26, 2008
SQL commnads :: Command prompt
1/ Go into command prompt , type
mysql -u username -p
it will prompt for password
then Enter password .
2/mysql> show databases;
+————————+
| Database |
+————————+
| information_schema |
| test_database |
| my_project |
| mephisto_development |
| mysql |
| webistrano_development |
+————————+
6 rows in set (0.13 sec)
3/use database database_name;
4/give the necessary SQL commands like select,alter,update etc.
I hope it was helpful in some way or the other.
Add comment March 17, 2008