Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename result set of COUNT(*) in SQL

Tags:

sql

sql-server

We were asked to create a SQL statement to show the count of customers whose country is USA. The column name of the result set should then be renamed into numcustomers.

The database is in here: http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all.

I already found the count(*) of customers in the USA:

SELECT * 
FROM Customers
WHERE Country = 'Germany'

But I can't figure out how to rename COUNT(*) into numcustomers. Any help?

like image 736
Kyle Sanchez Avatar asked Sep 19 '16 04:09

Kyle Sanchez


People also ask

How do I name a COUNT column in SQL?

If we want to change the 'city' column with the new name 'city_name' of this table, we can use the above-specified SQL Server syntax or stored procedure as follows: EXEC SP_RENAME 'Student. city', 'city_name', 'COLUMN'

How do I rename the results column in SQL?

1. Renaming a column name using the ALTER keyword. Line 2: RENAME COLUMN OldColumnName TO NewColumnName; For Example: Write a query to rename the column name “SID” to “StudentsID”.

What does COUNT (*) do in SQL?

COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.

How do you rename an attribute in SQL?

*Syntax may vary in different databases. Syntax(Oracle,MySQL,MariaDB): ALTER TABLE table_name RENAME TO new_table_name; Columns can be also be given new name with the use of ALTER TABLE.


2 Answers

SELECT count(*) AS numcustomers FROM Customers
WHERE Country = 'USA'

Use AS keyword to set column alias.

like image 146
Akshey Bhat Avatar answered Oct 06 '22 07:10

Akshey Bhat


Akshey Bhat is right

SELECT 
    count(*) AS numcustomers 
FROM Customers
WHERE Country = 'USA'

This query to get a number of usa customer and this index name is numcustomers or column name is numcustomers

like image 22
Pinakin Shobhana Avatar answered Oct 06 '22 08:10

Pinakin Shobhana