Difference between ID and Class – CSS

Yesterday one of my colleagues(a fresher) asked me difference between ID and class – CSS selectors. I explained him basic difference but he was still curious so I googled a bit about this. Believe me, I didn’t know all the differences between ID and Class. So I tried to sum up the difference between CSS ID and Class. I hope these will be helpful to you.

  1. Id begin with “#” and Class begin with “.”
    For example ID is #newID
    {width:100px;}
    Class is .newClass
    {width:200px;}
  2. ID is unique but Class is not
    This is known to most of as a hard coded rule but few of us know the exact meaning of this. It simply implies that in case of ID ‘Each element can have only one ID’ and ‘Each page can have only one element with that ID’ where in case of Class ‘You can use the same class on multiple elements’ and ‘You can use multiple classes on the same element’.
    For example ID < div ID=”newID”> </div>
    Class < div ID=”newClass”> </div>
    < div ID=”newClass”> </div>
    < div ID=”newClass”> </div>
  3. Browsers are equally compatible with ID and Class
    At times, we have a project working perfectly OK by using IDs where as same functionality don’t works on some other project where we used classes. As a fresher, my first thought on these scenarios was that class is causing problem and I would convert all the classes to ID. But, over the years, I learned that replacing ID with class(and vice versa) don’t helps, provided you were earlier using these correctly.
  4.  An element can have both: ID and Class
    An element can have both ID and Class but ID have higher preference over class.
    For example < div class=”redBackgroundColor” id=”grayBackgroundColor”> </div>
    In this case the background color of div will be gray

A commonly asked question is ‘What if I used an ID twice on a page. Will application crash or something’. The answer is that the application don’t behaves abnormally but it won’t pass the CSS Validation which is a must for all the websites for good ranking on search engines
Also if we have to use JavaScript for any element, then we have to enforce unique ID for element otherwise typescript won’t work as JavaScript methods(e.g. getElementById) won’t be picking up the correct element.

Now the real question, Should one use ID or Class ? . The answer to this question is simple, for the information that will be reused on should use class and for the information that is unique(and will not be reused) use an ID
For example, class should be used for tablerows, table cells etc. ID should be used for header, footer etc and for elements that are to be accessed in JavaScript.

Funny Hindi to English translations by kids

The conversational language at our home is Hindi. My kids are quite good at it. And everything was perfectly normal until one day the school teacher asked them to start speaking English. Although it was a uphill task, but I am proud to say that after few months of efforts my kids have started speaking English. But the English they speak seems extra territorial for a English speaker. I am sharing some of the funny Hindi to English translations done by my kids.

Hindi: Mera mann kar raha hai.
Kid’s English: My mann is doing

Hindi: Main aapko bataungi, mujhe kya kya chahiye.
Kid’s English: I tell you, I what what want.

Hindi: Mujhe sunai nahin diya.
Kid’s English: I am not listen.

Hindi: Aap bataoge.
Kid’s English: You tell karoge.

Hindi: Mera hath nahin pahunch raha.
Kid’s English: My hands are not coming.

Hindi: Mera teddy mat lo.
Kid’s English: My not take teddy.

Hindi: Woh kisne bahar nikala.
Kid’s English: Who that take out.

Hindi: Yeh nahin.
Kid’s English: This not.

Hindi: Kya hai?
Kid’s English: What is?

UNION Vs UNION ALL – Sql Server

The difference between Union and Union All if one of the things which not only confuse amateurs but also veterans.Although they both appears to be similar as both are used to club together the data from different sets but hey are quite dissimilar based on result and the performance.

UNION: UNION returns the unique data from different sets joined using the UNION. It removes duplicate rows.

UNION ALL: UNION ALL returns whole data from different sets . It don’t removes duplicates

Example: Create two tables CSS and JavaScript by using the sql server queries listed below

CREATE TABLE CSS
(ID INT,
firstName VARCHAR(50)
)

CREATE TABLE JavaScript
(ID INT,
firstName VARCHAR(50)
)

INSERT INTO CSS(ID, firstName) VALUES(1,’Bob’)
INSERT INTO CSS(ID, firstName) VALUES(2,’Smith’)
INSERT INTO CSS(ID, firstName) VALUES(3,’Randy’)
INSERT INTO CSS(ID, firstName) VALUES(4,’Nate’)
INSERT INTO JavaScript(ID, firstName) VALUES(1,’Bob’)
INSERT INTO JavaScript(ID, firstName) VALUES(2,’Patrice’)
INSERT INTO JavaScript(ID, firstName) VALUES(3,’Nate’)
INSERT INTO JavaScript(ID, firstName) VALUES(4,’Alex’)
INSERT INTO JavaScript(ID, firstName) VALUES(5,’Rammy’)

UNION:
SELECT * FROM CSS
UNION
SELECT * FROM JavaScript

The result would be:

Union in SQL Server - As I Had It

Union in SQL Server


The result returned has 8 rows and none of the rows is duplicate

UNION ALL:
SELECT * FROM CSS
UNION ALL
SELECT * FROM JavaScript

The result would be:

The result returned has rows and it has duplicate row(row with firstName as Bob)

Union All in SQL Server - As I Had It

Union All in SQL Server


Notes:
1. For Union or Union All, it is mandatory that all the involved tables have exactly same number of columns. The tables CSS and JavaScript used above have 2 columns each.
2. The datatypes of the columns of involved tables should be same(in the order of the columns), otherwise the implicit data type conversions will cause queries to return error. To handle the error, we will have to do explicit data type conversions

SELECT COL1, CONVERT(VARCHAR(23), COL2, 121) AS COL2 FROM T1
UNION ALL
SELECT COL1, CAST(COL2 AS VARCHAR(10)) FROM T2

From performance viewpoint, UNION ALL is superior to UNION. It is because UNION has to remove the duplicate rows. So one should use UNION only at places where input tables contain duplicate rows and the programmer is not supposed to display duplicate data. It is used by programmers because they do not understand the difference between these two operators

The difference in performance can be seen by having at look at the sql server execution plan of the queries used above(for tables named CSS and JavaScript):

SQL Server execution plan Union Vs Union All

Union Vs Union All execution plan


Here we see that UNION is quite costly as compared to UNION ALL because UNION has taken up 73% cost where as UNION ALL consumed only 23% cost. The extra burden in case of UNION is due to sorting the data and removing duplicates from the result set.
So we should always try that we use UNION only when its absolutely required. Otherwise UNION ALL is a better option, it gives faster results.

Inner Join VS Intersect in SQL Server

Last week I was working on reporting. QA team logged a bug that was not getting reproduced on development environment. First I reviewed the code at development, but was unable to figure the reason of count mismatch with details. Then I got the code reviewed by one of my seniors. He suggested me to replace inner join with intersect in a couple of conditions. I, till that day, used to this consider inner join and intersect as similar. But to my amazement both are completely different. So sharing my findings about difference between inner join and intersect:

Let’s have 2 different tables and we want the distinct rows which are common in both the tables what will we do to get them? Most of us will answer that they will use INNER JOIN to get the common rows of two tables, right? Wrong!!!!!!

This is a big misconception that INNER JOIN will always return all the common rows between two tables. In reality, INNER JOIN treats two NULLS as two different values rather than a same value so if you are joining a row based on a NULLable column there is a chance that if both tables have NULLs in that column then INNER JOIN will ignore those rows because

Two NULLs are not same in TSQL

To correctly retrieve all common rows between two tables, SQL Server 2005 has introduced the INTERSECT keyword. INTERSECT treats two NULLs as a same value and it returns all rows which are common in both the tables.
Additionally INNER JOIN retrieves all the records from the left table and all the records from the right table. Carefully observing we can notice many of the records as duplicate records. When INNER JOIN is used it gives us duplicate records, but that is not in the case of INTERSECT operator.
The duplicate records in UNION can be removed by using DISTINCT clause in SELECT statement. DISTINCT removes the duplicate rows and final result is exactly same in INTERSECT. In this way, INNER JOIN can simulate with INTERSECT when used with DISTINCT.

The results of an UPDATE statement are undefined if the statement includes a FROM clause that is not specified in such a way that only one value is available for each column occurrence that is updated (in other words, if the UPDATE statement is not deterministic). For example, given the UPDATE statement in the following script, both rows in table s meet the qualifications of the FROM clause in the UPDATE statement, but it is undefined which row from s is used to update the row in table t.

There aren’t many articles comparing performance between the two (UNION and INTERSECT) because they’re meant to substitute one for the other. INTERSECT is meant to find the distinct values combining two queries. That’s a very different goal than the INNER JOIN which is not finding distinct values, but values that simply match between two tables, derived or otherwise.

As a matter of fact, it’s worse than that since the INTERSECT operation requires that the values returned by both queries match in data type, so you couldn’t even do a normal INNER JOIN operation between disparate tables using INTERSECT.

But with similar data intersect takes a little less effort

Down with fever(may be chickenpox)……can I drink alcohol?

If you are down with chickenpox and googling to know if you can drink alcohol during chickenpox, then you are at the right page my friend. I have been in your shoes some time ago, my friend. So I am sharing my experience and findings during the time I had chickenpox.

Due to hectic weekend, I felt a bit dizzy on Monday morning. Initially I didn’t took it seriously considering it as Monday blues 🙂 . But by the evening I was quite sure that I am going down with fever. I usually avoid visit to clinics over minor illness, so I took crocin and a good sleep.

Then I realized that one of my roommates had got chickenpox two weeks ago. I decided not to delay the visit to doctor’s clinic. And to my worst thoughts, the doctor told that I have the symptoms of chicken pox I should start the medication for fever. The only nice word he said was that the chicken pox can only confirmed by tomorrow evening as I had not started to have scars as on today. So hoping for the best. But one thing I learned from the thoughts coming to my mind are that although I was afraid that the chickenpox would be quite troublesome.
But somewhere in my mind I was happy that I would be getting good 7-10 days to stay at my hometown. As it has been around 2 years that I have not stayed at my hometown for more than 2-3 days.

Anyways all was Ok until doctor uttered those ******* words  Do u drink or smoke?   I replied  no smoking but I do drink Ok, then don’t drink for next 5 days at least even if its not chickenpox.
Avoiding alcohol was a big issue for me as a couple of my friend were coming over to my place. So I went to the God of our generation , one and only one google.com. I tried to search but gone not get much. Only conclusion I can draw for the posts on google was:

Before drinking alcohol during chickenpox, you should consider the following:
  1. Your body should not be prone to allergies as alcohol can cause allergy.
  2. Alcohol generally lowers your immunity. So consuming alcohol during chickenpox can delay your recovery time.
  3. Doctor will most probably put you on antibiotics. The antibiotics don’t mix well with alcohol.
  4. Under the influence of alcohol, you can try to soothe the itching by rubbing the blisters therefore causing further implications.

So at the end of the day we can conclude that one should avoid drinking alcohol during chickenpox.