Ask Programiz - Human Support for Coding Beginners

Explore questions from fellow beginners answered by our human experts. Have some doubts about coding fundamentals? Enroll in our course and get personalized help right within your lessons.

  • All
  • Python
  • C
  • Java
  • CPP
  • SQL
  • JS
  • HTML
  • CSS
Abhay Jajodia
Expert
yesterday
Abhay Jajodia answered

Hi Rishabh! That's a great question and a common point of confusion for many learners just starting with SQL. Let's dive into it:

Why Use VARCHAR for Phone Numbers?

Even though phone numbers consist of digits, we treat them differently from regular numbers like age or ID. Here's why:

  • Fixed Format: Phone numbers often have leading zeros or country codes, and using an int would strip those away (e.g., +04123456789).

  • Non-Numeric Characteristics: Since phone numbers aren't used for calculations (like operations or comparisons), they are better stored as strings.

Give it a try and let me know if you run into any issues or have further questions. Happy querying! 🙂

SQL
This question was asked as part of the Learn SQL Basics course.
N
Expert
2 days ago
Nisha Sharma answered

If the table already exists but the columns are different, CREATE TABLE IF NOT EXISTS won’t try to “fix” or update anything. It simply checks: does a table named Customers already exist? If yes, it stops right there.

So what this really means is:

  • No error is thrown

  • No changes are made

  • The existing table stays exactly as it is, even if your new CREATE TABLE statement has different columns

If you actually want to change the table’s structure (add/remove columns, change data types, etc.), you’d use something like ALTER TABLE, or you’d drop and recreate the table if that’s appropriate for your setup.

Hope that helps. Feel free to reach out if you have any more questions.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
yesterday
Abhay Jajodia answered

Hi there! Wildcards in SQL make it easier to search for data when you don’t know the exact value.

Different databases use slightly different wildcard symbols, so here’s a quick and easy explanation for SQL Server, MS Access, and SQLite.

In SQL Server:

  • The % symbol matches any number of characters. For example, 'a%' finds values that start with “a”.

  • The _ symbol matches exactly one character, so 'a_' finds two-letter values starting with “a”.

  • You can also use square brackets like [a-f] to match any single character between “a” and “f”.

  • To exclude characters, use [^a-f] or [!a-f], which means anything not in that range.

In MS Access, the wildcards are slightly different:

  • The * symbol works like % and matches any number of characters.

  • The ? symbol represents exactly one character.

  • The # symbol matches a single digit (0–9).

  • You can also use brackets like [abc] to match a, b, or c, and ![abc] to match anything except those letters.

In SQLite, wildcards work the same way as in SQL Server:

  • % matches any number of characters

  • _ matches a single character.

All of these wildcards are used with the LIKE operator to filter results more flexibly depending on your database system.

For more detailed explanations and examples of each wildcard, you can check this link:

https://dbschema.com/blog/tutorials/sql-wildcard-characters/#:~:text=In%20SQL%2C%20a%20wildcard%20character,specified%20pattern%20in%20a%20column.

Hope this helps! Let me know if you’d like more examples or support.

SQL
This question was asked as part of the Learn SQL Basics course.
Udayan Shakya
Expert
2 weeks ago
Udayan Shakya answered

Hi there!

The problem statement is instructing you to select those rows from the Products table whose price is between 145 and 220, BUT the query should not select those rows whose price is either exactly 145 or 220.

This is basically a trick question because if you use the following query, then your solution will be wrong:

SELECT *
FROM Products
WHERE price BETWEEN 145 AND 220;

It's because BETWEEN will also select those rows whose price is 145 or 220.

So if you want to solve this problem using the BETWEEN operator, you need to use BETWEEN 146 AND 219.

This way, you've successfully excluded 145 and 220.

Correct Solution

SELECT *
FROM Products
WHERE price BETWEEN 146 AND 219;

Hope this helps! Let me know if you have more questions.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hello Omar, really nice question.

The logic behind SELECT in SQL is actually pretty simple once you see what problem it solves. A database table is like a giant spreadsheet. Most of the time, you don’t need every single piece of information in that table — you just need certain columns or certain rows.

SELECT is how you tell the database: “Show me only the parts I care about.”

For example, if a table has ten different columns but you only want to see age and country, you can write:

SELECT age, country
FROM Customers;

Now the database gives you just those two columns instead of the whole table. It’s efficient, it’s cleaner, and it makes it easier to work with your data.

So the real logic is:
Ask for the specific data you want instead of taking everything.

If you want to dig deeper into filtering, sorting, or selecting rows, I’m happy to help you explore that next.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hello Omar, really nice question.

Using * in SQL grabs every column in the table. It works, but it often gives you more data than you actually need. When you switch to something like:

SELECT age
FROM Customers;

you’re telling the database exactly what you want. That makes your query faster, easier to understand, and avoids pulling in extra or sensitive information by accident.

So the idea is simple:
Only ask for the columns you actually need.
It keeps the query clean and efficient.

If you have further questions, I'm here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hello Rishabh, really nice question.

When you use GROUP BY, you’re asking SQL to take many rows and squeeze them into one row per group. In your case, the groups are countries. That means all customers from the same country get combined so you can calculate something about the whole group — like the average age.

Here’s the key idea:
A grouped result needs one clear value per group.
The country has one value, the average age has one value
 but the name does not. There are many customer names in each country, so SQL doesn’t know which one to show.

That’s why this works:

SELECT country, AVG(age) AS average_age
FROM Customers
GROUP BY country;

But adding a name does not make sense unless you also group by it or aggregate it. If you tried:

SELECT country, name, AVG(age)

SQL would ask: “Which name should I show for this country? There are many.”

Once you see it that way, it becomes clear:
GROUP BY is for summaries, not individual details.

If you have further questions, I'm here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Kelish Rai
Expert
last month
Kelish Rai answered

Hello OTM Egytrans, really nice question.

In SQL, DISTINCT doesn’t belong to just one column once you write it — it applies to the entire set of selected columns together.

So:

SELECT DISTINCT col1
FROM your_table;

gives you unique values of col1.

But:

SELECT DISTINCT col1, col2
FROM your_table;

now treats each (col1, col2) pair as a single combination. SQL removes duplicate rows where both col1 and col2 are the same. It does not make col1 distinct while allowing col2 to vary freely in that same query.

That means there’s no direct way to say “make column 1 distinct, but don’t apply DISTINCT to column 2” in one simple SELECT DISTINCT col1, col2 statement.

If you want “distinct by column 1” and still show something from column 2, you usually:

  • either decide which row per col1 you want (for example, with MIN(col2) or MAX(col2) plus GROUP BY col1), or

  • use more advanced techniques (like window functions) depending on your database.

But the key idea is:
DISTINCT always works on the whole selected row, not just a single column unless you only select that one.

If you have further questions, I'm here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi Rishabh,

Good question — I see where the confusion is coming from.

Your query is counting how many distinct brands exist in the filtered data. Since you're filtering for ByteCore and ZapTech, there are clearly two distinct brands. So COUNT(DISTINCT brand) should return 2 — and it does.

But if you're getting 3 as the result, then the issue might be that you’re actually counting distinct product names, not brands.

If your intention is to count unique products under those brands, you should change the query to this:

SELECT COUNT(DISTINCT name) AS distinct_product  
FROM Products  
WHERE brand = 'ByteCore' OR brand = 'ZapTech';

This will count the number of different product names from those two brands — for example: keyboard, mouse, and headphone — which would give you 3.

So check what exactly you’re trying to count: brands or product names. That makes all the difference.

If you have more questions, I’m here to help.

SQL
This question was asked as part of the Learn SQL Basics course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi Bruno,

Great question. Both NOT and <> / != can be used to exclude values, but there are cases where NOT is preferred for clarity.

Here’s when using NOT can make more sense:

  1. With complex conditions
    When you're excluding entire conditions or expressions, NOT can make things clearer:

    WHERE NOT (age < 18 OR country = 'UAE')
    
  2. With subqueries
    NOT IN and NOT EXISTS are common and often easier to read than alternatives:

    WHERE NOT EXISTS (SELECT * FROM ...)
    
  3. Improved readability
    In some cases, AND NOT country = 'UAE' might be easier to read than country != 'UAE', especially in long conditions.

That said, if you're just comparing single values, <> or != is shorter and works just as well:

WHERE country != 'UAE'

In the end, it comes down to readability and personal or team preference. Functionally, both work — pick the one that makes your intent clearest.

If you’ve got more questions, I’m here to help.

SQL
This question was asked as part of the Learn SQL Basics course.